Michel Pigassou
2011-Apr-04 16:55 UTC
Best way to instanciate an empty address element in many nested fields_for
Hi! I have 3 models, User, Company and Address. Basically, a User belongs to a Company and an Address has one Company (because it can be linked to other models). In a form I want to display a User, its Company and its company Address, with fields_for. The problem is when my User has a Company, that does *not* have an Address: the part containing the Address is not displayed. I see 2 ways of instanciating my empty adress: 1) In the view: f.fields_for :company, @user.company do |company_form| and company_form.fields_for :address, (address = @user.company.address ? address : Address.new) 2) In the controller @user.company.address = Address.new unless @user.company.address (a variant would be to write @user.company.build_address, which I prefer over Address.new.) Do you see other ways and what do you think is best? Thanks. -- 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.
Garrett Lancaster
2011-Apr-04 17:08 UTC
Re: Best way to instanciate an empty address element in many nested fields_for
I would use @company.build_address unless @company.address Garrett Lancaster> ------------------------------------------------------------------------ > > Michel Pigassou <mailto:dagnan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > April 4, 2011 11:55 AM > > > Hi! > > I have 3 models, User, Company and Address. > Basically, a User belongs to a Company and an Address has one Company > (because it can be linked to other models). > > In a form I want to display a User, its Company and its company > Address, with fields_for. > The problem is when my User has a Company, that does *not* have an > Address: the part containing the Address is not displayed. > > I see 2 ways of instanciating my empty adress: > > 1) In the view: > f.fields_for :company, @user.company do |company_form| > and company_form.fields_for :address, (address = @user.company.address > ? address : Address.new) > > 2) In the controller > @ user.company.address = Address.new unless @ user.company.address > > (a variant would be to write @user.company.build_address, which I > prefer over Address.new.) > > Do you see other ways and what do you think is best? > > Thanks. > -- > 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.-- 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.