ryanb / nested_form

Rails plugin to conveniently handle multiple models in a single form.
MIT License
1.79k stars 505 forks source link

Allow custom wrapper selector #278

Open stsc3000 opened 10 years ago

stsc3000 commented 10 years ago

Nested_form assumes that the element wrapping the blueprint has class="fields" by default. As this may conflict with existing css, the following may be used to add a custom wrapper selector:

<%= nested_form_for @task do |f| -%>
  <%= f.text_field :name %>
  <table id="tasks">
    <%= f.fields_for :milestones, :wrapper => false do |milestone_form| %>
      <tr class="milestones-wrapper">
        <td><%= milestone_form.text_field :name %></td>
        <td><%= milestone_form.link_to_remove "Remove" %></td>
      </tr>
    <% end %>
  </table>
  <p><%= f.link_to_add "Add new milestone", :milestones, 
                                            "data-target" => "#tasks", 
                                            "data-selector" => ".milestones-wrapper" %></p>
<% end %>

If no custom selector is given, ".fields" is used. After rendering the blueprint, addFields or its prototype equivalent adds a data-nested-wrapper attribute to the wrapper so the remove links can find the wrapper, as they did with ".fields" previously.

Implemented for the prototype and jquery variants along with specs. Readme is updated, too.

douglasresende commented 9 years ago

:+1: