OK, so the relationship is: Program has many :events And each Event belongs to an Organizer (who has_many :events)
In a form for Program/edit, I'd like to list the associated events, and with each of those en开发者_运维技巧tries, also have access to each Organizer so I can do something like:
textfield(event.name) | textfield(event.date) | label(event.organizer.name)
So...
<% form_for([:admin, program]) do |f| %>
...
<h3>Event Data Fields</h3>
<table>
<% f.fields_for :events do |event_form| %>
<tr class="line_item">
<td><%= event_form.text_field :name %></td>
<td><%= event_form.text_field :date %></td>
<td><%= event_form.text_field "organizer.name", :disabled=>true %></td>
</tr>
<% end %>
</table>
<p><%= f.submit "Submit" %></p>
<% end %>
Obviously, event_form.text_field "organizer.name" won't work, but that's the concept
Why not just use fields for?...
<% form_for([:admin, program]) do |f| %>
...
<h3>Event Data Fields</h3>
<table>
<% program.events.each do |event| %>
<% f.fields_for event do |event_form| %>
<tr class="line_item">
<td><%= event_form.text_field :name %></td>
<td><%= event_form.text_field :date %></td>
<% event_form.fields_for(:organizer) do |organizer_form| %>
<td><%= organizer_form.text_field :name, :disabled=>true %></td>
<% end %>
</tr>
<% end %>
<% end %>
</table>
<p><%= f.submit "Submit" %></p>
<% end %>
Rails form helpers only work if you are working on one object at a time. If you want to update attributes of other records at the same time, you need to use text_field_tag for those fields, and include custom logic in the controller for interpreting/saving that data. IMHO, this is far from best practice.
Alternate solutions include using AJAX inline-editing, or if the idea is to be able to associate organizers with events when editing, to use a select field.
However, if organizers are so closely tied to events in your domain that you would want to edit their fields at the same time, why not just have organizer_name on your event object? At least, that would be my instinct without knowing more about your object model.
You can do it as halogenandtoast mentioned but if you would like only to show that field, you can also do it like this:
Instead of:
<td><%= event_form.text_field "organizer.name", :disabled=>true %></td>
put:
<td><%= event_form.object.organizer.name %> </td>
When you add object
to form builder it will refer to processed object, so you can access it's attributes.
精彩评论