Rails: Create a new entry in a model that belongs_

2019-08-29 09:28发布

问题:

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.

回答1:

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.


回答2:

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?