I'm using Rails 4.2 with cocoon gem
for building nesting form. Everything is saving and displaying just fine except when I want to make new person
through entities
form. Goal is to make new person on the fly at the same form where i will make new entity.
Here is code:
Models:
class Entity < ActiveRecord::Base
has_many :bonds
has_many :personas, :through => :bonds
accepts_nested_attributes_for :bonds, allow_destroy: true, reject_if: :all_blank
accepts_nested_attributes_for :personas, allow_destroy: false, reject_if: :all_blank
end
class Bond < ActiveRecord::Base
belongs_to :entity
belongs_to :persona
end
class Persona < ActiveRecord::Base
has_many :bonds
has_many :entities, :through => :bonds
accepts_nested_attributes_for :entities
end
Here is my form that works, except of when i try to add new person
to it.
/views/entities/_form.html.erb
<%= simple_form_for @entity do |f| %>
<h3>Info:</h3>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :number %>
<%= f.number_field :number %>
<h3>Bonds:</h3>
<div id="bonds">
<%= f.fields_for :bonds do |ff| %>
<%= render 'bond_fields', :f => ff %>
<% end %>
<div class="links">
<%= link_to_add_association 'add bond', f, :bonds %>
</div>
</div>
<%= f.submit %>
<% end %>
/entities/_bond_fields.html.erb
<div class='nested-fields'>
<%= f.label "Persona: " %>
<div class="persona">
<div class="persona_from_list">
<%= f.association :persona, :collection => Persona.order(:name), :prompt => 'Choose an existing owner' %>
</div>
<%= link_to_add_association "add a new person as owner", f, :persona %>
</div>
<%= f.label "how much: " %>
<%= f.number_field :how_much %>
<%= f.label "percent: " %>
<%= f.number_field :percent %>
<%= link_to_remove_association "remove bond", f %>
</div>
/entities/_persona_fields.html.erb
<div class='nested-fields'>
<%= f.label "Name: " %>
<%= f.text_field :name %>
<%= f.label "Code: " %>
<%= f.text_field :p_code %>
<%= link_to_remove_association "chose from list", f %>
</div>
I have added strong params everywhere so probably that's not the problem. Maybe you can help I have no ideas what is wrong!
Here is my params in /entities_controller
params.require(:entity).permit(:name, :number, :address, :registered_at,
:bonds_attributes => [:id, :persona_id, :entity_id, :percent, :date, :_destroy,
:personas_attributes => [:id, :name, :p_kods, :living ] ],
:personas_attributes => [:id, :name, :p_kods, :living ] )
Thanks anyways...
答案 0 :(得分:0)
仔细阅读服务器日志后,我看到有Unpermitted parameter: persona_attributes
。
因此解决方案只是在personas_attributes
中从persona_attributes
更改为EntitiesController
。奇怪但是轨道正在传递Persona单数形式的参数,而我写了多个。
希望它也能帮助别人......