Answer the question
In order to leave comments, you need to log in
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
<%= 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>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question