K
K
krokodily2015-07-27 21:50:44
JavaScript
krokodily, 2015-07-27 21:50:44

How to make a form with dynamic addition of fields for child objects, if two child objects also have a common child object?

There are four models:

class Festival < ActiveRecord::Base
  has_many :deadlines
  has_many :categories
end

class Deadline < ActiveRecord::Base
  belongs_to :festival
  has_many :fees
end

class Category < ActiveRecord::Base
  belongs_to :festival
  has_many :fees
end

class Fee < ActiveRecord::Base
  belongs_to :deadline
  belongs_to :category
end


I'm making a form for the Festival, inside which are fields for child objects.

<%= form_for @festival do |festival_form| %>

  <div id="deadlines_fields">
    <%= festival_form.fields_for :deadlines do |deadline_fields| %>
      <%= render 'deadline_fields', f: deadline_fields %>
    <% end %>
  </div>

  <%= link_to_add_association '', festival_form, :deadlines %>


  <div id="categories_fields">
    <%= festival_form.fields_for :categories do |category_fields| %>
      <%= render 'category_fields', f: category_fields %>
    <% end %>
  </div>

  <%= link_to_add_association '', festival_form, :categories %>

<% end %>


<!--deadline_fields.html.erb-->
<div class="nested-fields">
  <%= f.text_field :name %>
  <%= f.date_field :date %>
  <%= link_to_remove_association '', f %>
</div>


<!--category_fields.html.erb-->
<div class="nested-fields">
  <%= f.text_field :name %>
  <%= link_to_remove_association '', f %>

  <div id="fees_fields">
    <%= f.fields_for :fees do |fee_fields| %>
      <%= render 'fee_fields', f: fee_fields %>
    <% end %>
  </div>
</div>


<!--fee_fields.html.erb-->
<div class="nested-fields">
  <p><%= f.object.deadline.name %>:</p>
  <%= f.number_field :price %>
</div>


I need the following logic in a form. When adding a Deadline, Fees should be added to each Category. When submitting the form, the Category is saved normally, the Fees nested in the Category are also saved correctly, but the Fee does not have a deadline_id. What can I do to save Fees as both a Category child and a Deadline child at the same time?

In theory, for this we need a hidden field with name="festival[categories_attributes][0][fees_attributes][0][dedline_id]", but until the form is submitted, the deadline_id has not yet been assigned. What do they do in this case?

update. I re-read my question, perhaps it was not clear what should happen in the form. link_to_add_association and link_to_remove_association are helpers that render a link that, when clicked, will add/remove fields for the new object. When adding fields for Deadline, new fields for Fee should be added. How can I specify in the new fields for Fee that this new Fee should belong to the new Deadline?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Vsk, 2015-07-28
@viktorvsk

api.rubyonrails.org/classes/ActiveRecord/NestedAtt...

T
Timur Talipov, 2015-07-28
@ClickF1

AngularJS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question