Rails 跟着敏捷开发那本书敲的时候 出现个出错,看提示也没搞懂求教

limkurn · October 19, 2012 · Last by limkurn replied at October 19, 2012 · 3467 hits

ActiveModel::MassAssignmentSecurity::Error in LineItemsController#create

Can't mass-assign protected attributes: product Rails.root: /home/lim/work/depot

Application Trace | Framework Trace | Full Trace app/controllers/line_items_controller.rb:45:in `create'

create 方法代码如下 def create @cart = current_cart product=Product.find(params[:product_id]) @line_item = @cart.line_items.build(:product => product)

respond_to do |format| if @line_item.save format.html { redirect_to(@line_item.cart, :notice => 'line item was successfully created.')} format.xml {render :xml => @line_item, :status => :created ,:location => @line_item} else format.html {render :action => "new"} format.xml {render :xml => @line_item.errors, :status => :unprocessable_entity} end end end

由于你写了“@cart.line_items.build(:product => product)”这句语句 这里要求你必须把 product 属性放入 attr_accessible 中 请参考: http://api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/ClassMethods.html

@line_item = @cart.line_items.build(:product_id => product)

#2 楼 @gonglexin 昨天它提示在 45 行 然后就看这句看半天 没发现错误 蛋疼死我了

You need to Sign in before reply, if you don't have an account, please Sign up first.