Rails: Create a new entry in a model that belongs_to two other models

≯℡__Kan透↙ 提交于 2019-12-12 05:08:39

问题


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?



来源:https://stackoverflow.com/questions/12013648/rails-create-a-new-entry-in-a-model-that-belongs-to-two-other-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!