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.
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? %>
<div id="error_explanation">
<h2><%= pluralize(@person.errors.count, "error") %> prohibited
this person from being saved:</h2>s
<ul>
<% @person.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= person_form.label :name %><br />
<%= person_form.text_field :name %>
</div>
<div class="field">
<%= person_form.label :email %><br />
<%= person_form.text_field :email %>
</div>
<div class="field">
<%= person_form.label :phone %><br />
<%= person_form.text_field :phone %>
</div>
<h1>outside</h1>
<%= 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.
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?
> 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?
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.
>> > 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?
> 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.
thank you. my bad, consider this is resolved. It works for me
now.