Ruby on Rails form building simplification
thanks for taking the time to read this.
I'm trying to build a form that accepts multiple members. It starts off with 3 members and then there's a javascript button that adds a member, with a maximum of 100 members. The only way I can figure out how to do it is by hard coding all the form elements as below 1..100 (I only showed 1-4) and then hiding all the extras. The javascript button then just displays the next element. Is there a way I can generate the form element in the javascript? Or is there a way to use a loop to create the 100 hard coded elements?
<tr id="member_1">
<td></td>
<td class="field"><%= text_field(:members, :member_1_name, :value=>@members[:member_1_name]) %></td>
<td class="field"><%= text_field(:members, :member_1_number, :value=>@members[:member_1_number]) %></td>
</tr>
<tr>
<td></td>
<td class="heading">Member Names</td>
<td></td>
</tr>
<tr id="member_2">
<td></td>
<td class="field"><%= text_field(:members, :member_2_name, :value=>@members[:member_2_name]) %></td>
<td class="field"><%= text_field(:members, :member_2_number, :value=>@members[:member_2_number]) %></td>
</tr>
<tr id="member_3">
<td></td>
<td class="field"><%= text_field(:members, :member_3_name, :value=>@members[:member_3_name]) %></td>
<td class="field"><%= text_field(:members, :member_3_number, :value=>@members[:member_3_number]) %>&开发者_开发技巧lt;/td>
</tr>
<tr id="member_4" style="display:none">
<td></td>
<td class="field"><%= text_field(:members, :member_3_name, :value=>@members[:member_3_name]) %></td>
<td class="field"><%= text_field(:members, :member_3_number, :value=>@members[:member_3_number]) %></td>
</tr>
Thanks Again! Let me know if something isn't clear and whatnot
This sort of functionality is covered in episodes 196 & 197 of RailsCasts
Nested Model Forms Part 1 | Nested Model Forms Part 2
You should create only first member and put hidden div with template on a page. Javascript button should use this template and insert new field with it. See for example: http://embeddedjs.com/
精彩评论