我使用has_and_belongs_to_many到has_and_belongs_to_many設置有一個嵌套的表格(使用瑞安B的nested_form寶石):創建Rails中HABTM嵌套形式的新紀錄
Opening has_and_belongs_to_many :contacts
Contact has_and_belongs_to_many :openings
當試圖添加新聯繫人的開口,在這種情況下,我得到:
Can't mass-assign protected attributes: new_1346666966632
爲
"opening"=>{"contacts_attributes"=>{"new_1346666966632"=>{"contacts"=>{"name"=>"Test Contact",
我已經添加了相應的 「accepts_nested_attributes_for」 和 「attr_accessible」,並正在建設即@ opening.contacts.build和@ opening.contacts.build(PARAMS接觸[:開放] [:contact_attributes])。
我哪裏錯了?在這裏通過關係使用has_many會更好嗎?
編輯:
查看:
<%= simple_nested_form_for @opening, :wrapper => :plain do |f| %>
<%= f.link_to_add "Add a contact", :contacts %>
<%= f.button :submit %>
<% end %>
它採用了局部產生嵌套的聯繫人字段:
<%= f.fields_for :contacts, @opening.contacts.build do |contact_form| %>
<%= contact_form.input :name, :label => false, :input_html => { :class => 'span6' } %>
<%= contact_form.input :company, :label => false, :input_html => { :class => 'span6' } %>
<%= contact_form.input :telephone, :label => false, :input_html => { :class => 'span6' } %>
<%= contact_form.input :email_address, :label => false, :input_html => { :class => 'spa12' } %>
<% end %>
嘗試添加attr_accessible:contacts_attributes和檢查 – Amar
對不起,忘了提,我已經得到了太多 –
添加您的'view'碼也 – Samiron