Я использую Rails 4.2 с cocoon gem
для построения формы гнездования. Все сохраняет и отображает только штрафы, за исключением случаев, когда я хочу создать новую форму person
через форму entities
. Цель состоит в том, чтобы сделать нового человека «на лету» в той же форме, где я создам новый объект.Вложенная форма в Rails не сохраняется
Вот код:
Модели:
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
Вот моя форма, которая работает, за исключением того, когда я пытаюсь добавить новый person
к нему.
/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>
Я добавил сильные параметры везде, поэтому, вероятно, это не проблема. Может быть, вы можете помочь, у меня нет идей, что не так!
Вот мой PARAMS в/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 ])
Спасибо в любом случае ...
Сильного PARAMS все еще может быть проблемой, но нельзя сказать, если вы не показать свои методы Params к нам. – MarsAtomic
У вас есть ': personas_attributes => [...]' в ваших сильных параметрах 'entities_controller'? – Zoran
yes У меня есть ': personas_attributes' в entity_controller. Я добавил к моему вопросу все сильные параметры. вы можете взглянуть. – DanielsV