Forum: Ruby on Rails How can I use aggregation objects in form_for?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
1292dfc5df2064e008f7f12b9c56561d?d=identicon&s=25 allen (Guest)
on 2007-04-13 06:47
(Received via mailing list)
Say I have a person object which has a aggregation object call
address. The the code to manipulate a form for create a new person
should be:

# new.rhtml
<% form_for :person, :url => {:action => 'create'} do |f| %>
  <%= render :partial => 'form', :locals => {:f => form} %>
  <%= submit_tag "Create" %>
<% end %>

# _form.rhtml
<p><label>Name</label><br/><%= form.text_field :name  %></p>
<p><label>Country</label><br/><%= form.text_field :country  %></p>
<p><label>City</label><br/><%= form.text_field :city  %></p>

This does work. But country and city belong to the aggregation object
address and I want to use address in the form. According to RDoc and
rails book. The above text_field helper method will generate <input
value="person[name]" /> and rails will recognize it and translate it
into :person => {:name}. And if I can generate something like <input
value="person[address][country]" />, then rails will get :person =>
{:address => {:country}}, which is exactly what I want. How can I tell
rails to generate this kind of input for me? I cannot find any
information about this in RDoc. Thanks a lot.
D1f1c20467562fc1d8c8aa0d328def62?d=identicon&s=25 Florian Gilcher (skade)
on 2007-04-13 08:19
There is 'fields_for' which behaves the same as 'form_for' but without
wrapping the block in a form tag.

<%= fields_for @model.aggregation, ... do |subform|%>

<% end %>
This topic is locked and can not be replied to.