Consider a Store that has_many Products which have_many Opinions. Here are Models:
Store:
class Store < ActiveRecord::Base
attr_accessible :desc, :location, :name, :phone, :status, :url
has_many :products
has_many :opinions, :through => :products
end
Product:
class Product < ActiveRecord::Base
attr_accessible :desc, :name, :status, :url
belongs_to :store
has_many :opinions
end
finally, Opinion:
class Opinion < ActiveRecord::Base
attr_accessible :content, :eval, :status
belongs_to :store
belongs_to :product
end
To create a new opinion (that belongs to a product and a store), here is the create method of OpinionsController:
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
But here is the resulted error: Can't mass-assign protected attributes: product_id
Question: How can I pass the product_id to the controller?
Tell me if you need more info.
Thanks in advance.