0

我有 3 个模型;报价单、项目和产品。

我的 quote/new.html.erb 设置为呈现包含项目表单的部分,并在该项目表单中呈现部分以选择产品。

错误:ActiveModel::MassAssignmentSecurity::Error in QuotesController#create "Can't mass-assign protected attributes: products"

(我在下面编辑了不相关的东西) Quote.rb

class Quote < ActiveRecord::Base
   attr_accessible :items_attributes

   has_many :items, :dependent => :destroy
   accepts_nested_attributes_for :items
end

项目.rb

class Item < ActiveRecord::Base
  attr_accessible :price, :product_attributes

  belongs_to :quote
  belongs_to :product
  accepts_nested_attributes_for :product
end

产品.rb

class Product < ActiveRecord::Base
  attr_accessible :name, :item_make

  has_many :items
  accepts_nested_attributes_for :items
end

新的.html.erb

<%= simple_nested_form_for @quote do |m| %>

  <%= m.simple_fields_for :items, :html => { :multipart => true } do |quoteform| %>
    <%= render "form", f: quoteform %>
  <% end %>

  <%= m.link_to_add "Add an item", :items %>
  <%= m.button :submit %>
<% end %>

_form.html.erb

<%= f.simple_fields_for :products, :html => { :multipart => true } do |x| %>
    <% render "layouts/styleselect", g: x %>
<% end %>

_styleselect.html.erb

<% g.hidden_field :item_make, :value => @item.make %>
<%= g.input :name, collection: Product.where(:item_make => 1), label: false, input_html: {:id=>"sst_style"} %>

所以基本上嵌套的形式是Quote->Item->Product,但是item属于product,这可能是导致问题的原因吗?我尝试将 product_attributes 或 products_attributes 添加到 item 模型和 quote 模型中,并与 accept_nested_attributes_for 产品相同。

任何帮助将不胜感激,谢谢。

4

1 回答 1

1

看起来你需要做products单数。

<%= f.simple_fields_for :product, :html => { :multipart => true } do |x| %>
    <% render "layouts/styleselect", g: x %>
<% end %>

您目前拥有:

<%= f.simple_fields_for :products, :html => { :multipart => true } do |x| %>
于 2013-10-03T03:03:58.983 回答