考虑一个拥有_many 产品的商店,其中有_many 意见。以下是型号:
店铺:
class Store < ActiveRecord::Base
attr_accessible :desc, :location, :name, :phone, :status, :url
has_many :products
has_many :opinions, :through => :products
end
产品:
class Product < ActiveRecord::Base
attr_accessible :desc, :name, :status, :url
belongs_to :store
has_many :opinions
end
最后,意见:
class Opinion < ActiveRecord::Base
attr_accessible :content, :eval, :status
belongs_to :store
belongs_to :product
end
要创建一个新意见(属于产品和商店),这里是 OpinionsController 的 create 方法:
def create
# Get product info.
product = Product.find_by_id params[:opinion][:product_id]
# Create a new opinion to this product
opinion = product.opinions.build params[:opinion]
opinion.status = 'ON'
if opinion.save
redirect_to :back, :notice => 'success'
else
redirect_to :back, :alert => 'failure'
end
end
但这是导致的错误:Can't mass-assign protected attributes: product_id
问题:如何将 product_id 传递给控制器?
如果您需要更多信息,请告诉我。
提前致谢。