Issue rendering fields_for in 1 to 1 mapping nested model form view

I am new to rails, coming from .net. I am having the issue for 1:1
mapping nested model view the form will not render.

I have the code below. note the address object is created. I dont see
the label and text box. the person form render without any issue. the
debug works outside the fields_for.

Any help is appreciated

thanks
Geo

Model

class Person < ActiveRecord::Base
has_one :address
accepts_nested_attributes_for :address
end

class Address < ActiveRecord::Base
belongs_to :person
end

Controller

GET /people/new

GET /people/new.xml

def new
@person = Person.new
@address = @person.address = @person.build_address

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @person }
end

end

View: _form.html.erb

<%= form_for(@person) do |person_form| %>
<% if @person.errors.any? %>


<%= pluralize(@person.errors.count, “error”) %> prohibited
this person from being saved:

s

    <% @person.errors.full_messages.each do |msg| %>
  • <%= msg %>

  • <% end %>


<% end %>
<%= person_form.label :name %>
<%= person_form.text_field :name %>
<%= person_form.label :email %>
<%= person_form.text_field :email %>
<%= person_form.label :phone %>
<%= person_form.text_field :phone %>

outside

<%= debug(@person.address.attributes) %> <%= debug(@person.address) %> <% person_form.fields_for @address do |address_form| %>

inside form

<%= address_form.label :addressline1 %>
<%= address_form.text_field :addressline1 %>
<%= address_form.label :addressline2 %>
<%= address_form.text_field :addressline2 %>
<% end %>
<%= person_form.submit %>
<% end %>

On 29 December 2010 07:52, Geo M. [email protected] wrote:

Geo
class Address < ActiveRecord::Base
@address = @person.address = @person.build_address
<%= form_for(@person) do |person_form| %>
<% end %>
<%= person_form.text_field :phone %>

outside

<%= debug(@person.address.attributes) %> <%= debug(@person.address) %> <% person_form.fields_for @address do |address_form| %>

I think that should be :address not @address, it specifies the
association to use.

Colin

I tried address still not able to display the address form.

<% person_form.fields_for :address do |address_form| %>

thanks
Geo

On 30 December 2010 04:32, Geo M. [email protected] wrote:

I tried address still not able to display the address form.

<% person_form.fields_for :address do |address_form| %>

Please don’t top post, insert your comments at appropriate points in
the previous message. It makes it easier to follow the thread.
Thanks.

What do you mean by not able to display address form? Is there an
error shown? If not then what does the generated html look like? If
the html is not correct then what is wrong with it?

Colin

On 30 December 2010 20:34, Geo M. [email protected] wrote:

the previous message. It makes it easier to follow the thread.
Thanks.

What do you mean by not able to display address form? Is there an
error shown? If not then what does the generated html look like? If
the html is not correct then what is wrong with it?

basically the code inside the <%
person_form.fields_for :address do |address_form| %> is not

It should be <%= person_form… %>, I should have seen that before.
Without the = it runs the code but does not put the result into the
page.

Colin

On Dec 30, 2:06am, Colin L. [email protected] wrote:

What do you mean by not able to display address form? Is there an
error shown? If not then what does the generated html look like? If
the html is not correct then what is wrong with it?

  basically the code inside the <%

person_form.fields_for :address do |address_form| %> is not
executed. i.e in the html page the label and edit boxes for address is
not displayed to the user. and if you look at view source you dont see
the html tags for them. if you move this code below inside fields_for,
no object information is displayed.

<%= debug(@person.address.attributes) %>
<%= debug(@person.address) %>

  if there is better way (work around) to handle one-one mapping.

please let me know as well.

On Dec 30, 2:03pm, Colin L. [email protected] wrote:

Thanks.
page.
thank you. :slight_smile: my bad, consider this is resolved. It works for me
now.