嵌套表单“无法批量分配受保护的属性”

时间:2013-10-02 22:47:03

标签: ruby-on-rails forms activerecord nested-attributes

我有3个型号;报价,项目和产品。

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

错误:ActiveModel :: MassAssignmentSecurity :: QuotesController中的错误#create “无法批量分配受保护的属性:产品”

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

class Quote < ActiveRecord::Base
   attr_accessible :items_attributes

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

item.rb的

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

  belongs_to :quote
  belongs_to :product
  accepts_nested_attributes_for :product
end

Product.rb

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

  has_many :items
  accepts_nested_attributes_for :items
end

new.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-&gt; Item-&gt; Product,但是item属于产品,这可能导致问题?我尝试将product_attributes或products_attributes添加到项目模型和报价模型中,并将accept_nested_attributes_for产品添加到产品模型中。

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

1 个答案:

答案 0 :(得分: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| %>