I have a nested model form in the style of this Railscast http://railscasts.com/episodes/196-nested-model-form-revised?view=asciicast
I need to give each tag a unique ID. Currently, each field generated has a unique ID and name given by a helper method that assigns a unique ID to every association record. So that is taken cared of. However, this form has "fieldset" tags which isn't assigned an ID. I need a unique ID for each fieldset for jQuery manipulation purposes.
Specifically, how do I do give each fieldset generated for an "Activity" record a unique CSS tag ID? Posted below is how my form is created. Thank you
_form.html.erb
<%= form_for(@trip) do |f| %>
<%= f.fields_for :days do |builder| %>
<%= render 'day_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add Day", f, :days %>
_day_fields.html.erb partial
<fieldset>
<%= f.label :summary, "Day Summary" %><br />
<%= f.text_area :summary, :rows => 1 %><br />
<%= link_to "remove", '#', class: "remove_fields" %>
<%= f.fields_for :activities do |builder| %>
<%= render 'activity_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add Activity", f, :activities %>
</fieldset>
_activity_fields.html.erb
<fieldset>
<%= f.label :title, "Activity" %><br />
<%= f.text_field :title, :rows => 1 %><br />
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
If you are using ryanb nested_form gem :
Use f.index
instead. And make sure that you pass every different form_builder in your partials.
For example if you do
<%= main_form.fields_for :nested1_items do |nested1_form| %>
<%= render partial 'main/nested1_fields', nested1_form: nested1_form %>
<% end %>
_nested1_fields
<fieldset id="nested1-<%= nested1_form.index %>-items">
See my answer in this question.
Why not just use the f.object
passed in to both partials?
<% fieldset_id = "#{f.object.class.underscore}_#{f.object.id}" %>
<fieldset id='<%= fieldset_id %>'>
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With