Geo Manickam
2010-Dec-29 07:52 UTC
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? %> <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| %> <h1>inside form</h1> <div class="field"> <%= address_form.label :addressline1 %><br /> <%= address_form.text_field :addressline1 %> </div> <div class="field"> <%= address_form.label :addressline2 %><br /> <%= address_form.text_field :addressline2 %> </div> <% end %> <div class="actions"> <%= person_form.submit %> </div> <% end %> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Dec-29 16:34 UTC
Re: Issue rendering fields_for in 1 to 1 mapping nested model form view
On 29 December 2010 07:52, Geo Manickam <geo_72-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> 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. Colin> <h1>inside form</h1> > <div class="field"> > <%= address_form.label :addressline1 %><br /> > <%= address_form.text_field :addressline1 %> > </div> > <div class="field"> > <%= address_form.label :addressline2 %><br /> > <%= address_form.text_field :addressline2 %> > </div> > <% end %> > <div class="actions"> > <%= person_form.submit %> > </div> > <% end %> > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Geo Manickam
2010-Dec-30 04:32 UTC
Re: Issue rendering fields_for in 1 to 1 mapping nested model form view
I tried address still not able to display the address form. <% person_form.fields_for :address do |address_form| %> thanks Geo On Dec 29, 8:34 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 29 December 2010 07:52, Geo Manickam <geo...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > > > > > > > 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. > > Colin > > > > > > > > > <h1>inside form</h1> > > <div class="field"> > > <%= address_form.label :addressline1 %><br /> > > <%= address_form.text_field :addressline1 %> > > </div> > > <div class="field"> > > <%= address_form.label :addressline2 %><br /> > > <%= address_form.text_field :addressline2 %> > > </div> > > <% end %> > > <div class="actions"> > > <%= person_form.submit %> > > </div> > > <% end %> > > > -- > > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Dec-30 10:06 UTC
Re: Re: Issue rendering fields_for in 1 to 1 mapping nested model form view
On 30 December 2010 04:32, Geo Manickam <geo_72-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> 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> > thanks > Geo > > On Dec 29, 8:34 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 29 December 2010 07:52, Geo Manickam <geo...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: >> >> >> >> >> >> >> >> >> >> > 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. >> >> Colin >> >> >> >> >> >> >> >> > <h1>inside form</h1> >> > <div class="field"> >> > <%= address_form.label :addressline1 %><br /> >> > <%= address_form.text_field :addressline1 %> >> > </div> >> > <div class="field"> >> > <%= address_form.label :addressline2 %><br /> >> > <%= address_form.text_field :addressline2 %> >> > </div> >> > <% end %> >> > <div class="actions"> >> > <%= person_form.submit %> >> > </div> >> > <% end %> >> >> > -- >> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org. >> > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Geo Manickam
2010-Dec-30 20:34 UTC
Re: Issue rendering fields_for in 1 to 1 mapping nested model form view
On Dec 30, 2:06 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 30 December 2010 04:32, Geo Manickam <geo...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> 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?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.> Colin > > > > > > > > > > > thanks > > Geo > > > On Dec 29, 8:34 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> On 29 December 2010 07:52, Geo Manickam <geo...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > >> > 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. > > >> Colin > > >> > <h1>inside form</h1> > >> > <div class="field"> > >> > <%= address_form.label :addressline1 %><br /> > >> > <%= address_form.text_field :addressline1 %> > >> > </div> > >> > <div class="field"> > >> > <%= address_form.label :addressline2 %><br /> > >> > <%= address_form.text_field :addressline2 %> > >> > </div> > >> > <% end %> > >> > <div class="actions"> > >> > <%= person_form.submit %> > >> > </div> > >> > <% end %> > > >> > -- > >> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > >> > To post to this group, send email to rubyonrails-talk@googlegroups.com. > >> > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Dec-30 22:03 UTC
Re: Re: Issue rendering fields_for in 1 to 1 mapping nested model form view
On 30 December 2010 20:34, Geo Manickam <geo_72-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> > > On Dec 30, 2:06 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> On 30 December 2010 04:32, Geo Manickam <geo...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> 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? > > > basically the code inside the <% > person_form.fields_for :address do |address_form| %> is notIt 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Geo Manickam
2010-Dec-30 22:29 UTC
Re: Issue rendering fields_for in 1 to 1 mapping nested model form view
On Dec 30, 2:03 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 30 December 2010 20:34, Geo Manickam <geo...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > > > > > > > > > On Dec 30, 2:06 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> On 30 December 2010 04:32, Geo Manickam <geo...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> 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? > > > 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.> > Colin-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.