Forum: Ruby on Rails Form_for for nested objects

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.
1a26da0ea2bbc3fdc436d43e14374854?d=identicon&s=25 Jerome Deflot (Guest)
on 2007-05-24 17:24

I have the following data model:
* Person (name, ...)
* Address (street, city)
* Person has_ony Address

I would like to edit the Person and its Address in one single form.
Using form_for, I can add the person fields. Using fields_for, I could
have the Address also, but it's not nested here (aka
person[address[street]]. So the question is how to "prefix" the
field_for to include the Address hash into the Person hash?

Thank you!
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-05-24 20:57
(Received via mailing list)
It doesn't appear that you can do that.  But, the solution is to take
care of the association in the create method.

See this example notice the use of a database transaction to properly
handle the rollback:
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
# We make no guarantees that this code is fit for any purpose.
# Visit for more book
class ProductsController < ApplicationController

  def new
    @product =
    @details =

  def create
    @product =[:product])
    @details =[:details])

    Product.transaction do
      @details.product = @product!!
      redirect_to :action => :show, :id => @product

  rescue ActiveRecord::RecordInvalid => e
    @details.valid?   # force checking of errors even if products
    render :action => :new

  def show
    @product = Product.find(params[:id])

On May 24, 11:24 am, Jerome Deflot <>
This topic is locked and can not be replied to.