My New Love: fields_for
March 21st, 2007
So I have always loved form_for, but it becomes tricky to use when your object is more complex than a simple flat object with attributes that can be set in the manner. But what if you are trying to save a model that has assoctiated models? Enter fields_for
class Package < ActiveRecord::Base
has_one :address
end
class Address <ActiveRecord::Base
end
<% form_for @package do |f| %>
<%= f.text_field 'special_notes' %>
<% f.fields_for @package.address do |a| %>
<%= a.text_field 'first_line' %>
<%= a.text_field 'second_line' %>
<% end %>
<% end %>
Sorry, comments are closed for this article.