0

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.

Hassen
  • 6,966
  • 13
  • 45
  • 65

2 Answers2

3

This looks like a scenario where you could use nested resource routing http://guides.rubyonrails.org/routing.html#nested-resources

However, if you just want a quick fix in your Opinion controller you just need to omit the product_id when building the Opinion. Something like this should work:

# Get product info.
product = Product.find_by_id params[:opinion].delete(:product_id)  # delete removes and returns the product id

# Create a new opinion to this product
opinion = product.opinions.build params[:opinion]  # Since there is no product id anymore, this should not cause a mass assignment error.
Wizard of Ogz
  • 12,543
  • 2
  • 41
  • 43
0

Opinion could not have belongs_to :store (you can get store params like opinion.product.store) so.. why you not showed the attr_accessible line in your controllers?

achempion
  • 794
  • 6
  • 17
  • I don't understand, achempion... `attr_accessible` is for models, no? I added all of `attr_accessible` in the question, but can you be more precise, please? – Hassen Aug 18 '12 at 08:47