Joshua Abbott
2008-Dec-02 22:29 UTC
Am I wrong or is fields_for not working right in Rails 2.2.2
I may be totally wrong about what the fields_for method is supposed to do, but for some reason I thought using it scoped an object around an association. For example, I have a User class and when you''re creating a new user I want to use fields_for to add a BillingAddress to the user like so: <%- fields_for :billing_address do |billing_address_form| -%> <%= render :partial => ''billing_addresses/form'', :locals => { :f => billing_address_form } %> <%- end -%> This however, is not scoping the user object around the billing address form fields because when I view source, I see this: <p> <label for="billing_address_address_one">Address one</label><br /> <input id="billing_address_address_one" name="billing_address[address_one]" size="30" type="text" /> </p> <p> <label for="billing_address_address_two">Address two</label><br /> <input id="billing_address_address_two" name="billing_address[address_two]" size="30" type="text" /> </p> I''m using Rails 2.2.2 and maybe this never worked the way I''m thinking it should, or I could be doing something wrong... Any suggestions anyone? Thanks, Josh -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-03 21:45 UTC
Re: Am I wrong or is fields_for not working right in Rails 2.2.2
On Dec 2, 10:29 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I may be totally wrong about what the fields_for method is supposed to > do, but for some reason I thought using it scoped an object around an > association. > > For example, I have a User class and when you''re creating a new user I > want to use fields_for to add a BillingAddress to the user like so: > > <%- fields_for :billing_address do |billing_address_form| -%> > <%= render :partial => ''billing_addresses/form'', :locals => { :f => > billing_address_form } %> > <%- end -%> > > This however, is not scoping the user object around the billing address > form fields because when I view source, I see this: > > <p> > <label for="billing_address_address_one">Address one</label><br /> > <input id="billing_address_address_one" > name="billing_address[address_one]" size="30" type="text" /> > </p> > <p> > <label for="billing_address_address_two">Address two</label><br /> > <input id="billing_address_address_two" > name="billing_address[address_two]" size="30" type="text" /> > </p> >Assuming that partial is along the lines of <%= f.label :address_one %> <%= f.text_field :address_one %> ... That''s what I would expect. Are you expecting it to magically guess something ?(if so, what?) Fred> I''m using Rails 2.2.2 and maybe this never worked the way I''m thinking > it should, or I could be doing something wrong... > > Any suggestions anyone? > > Thanks, > Josh > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Abbott
2008-Dec-03 21:50 UTC
Re: Am I wrong or is fields_for not working right in Rails 2
I guess I''m expecting it to know from <%- form_for(@user) do |f| -%> <%- fields_for :billing_address do |billing_address_form| -%> <%= render :partial => ''billing_addresses/form'', :locals => { :f => billing_address_form } %> <%- end -%> <%- end -%> to build inputs more like user[billing_address][address_two] Is that not what fields_for is used for? -- Josh Frederick Cheung wrote:> On Dec 2, 10:29�pm, Joshua Abbott <rails-mailing-l...@andreas-s.net> > wrote: >> <%- end -%> >> � <label for="billing_address_address_two">Address two</label><br /> >> � <input id="billing_address_address_two" >> name="billing_address[address_two]" size="30" type="text" /> >> </p> >> > Assuming that partial is along the lines of > <%= f.label :address_one %> > <%= f.text_field :address_one %> > ... > > That''s what I would expect. Are you expecting it to magically guess > something ?(if so, what?) > > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-03 22:03 UTC
Re: Am I wrong or is fields_for not working right in Rails 2
On Dec 3, 9:50 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I guess I''m expecting it to know from > > <%- form_for(@user) do |f| -%> > > <%- fields_for :billing_address do |billing_address_form| -%> > <%= render :partial => ''billing_addresses/form'', :locals => { :f => > billing_address_form } %> > <%- end -%> > > <%- end -%> > > to build inputs more like user[billing_address][address_two] >Just because lexically it''s ''inside'' the first form isn''t enough (or to put things another way, it''s hard for code to know about the code that''s calling them). If you want what you describe you need to be explicit: use f.fields_for Fred> Is that not what fields_for is used for? > > -- Josh > > > > Frederick Cheung wrote: > > On Dec 2, 10:29 pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > >> <%- end -%> > >> <label for="billing_address_address_two">Address two</label><br /> > >> <input id="billing_address_address_two" > >> name="billing_address[address_two]" size="30" type="text" /> > >> </p> > > > Assuming that partial is along the lines of > > <%= f.label :address_one %> > > <%= f.text_field :address_one %> > > ... > > > That''s what I would expect. Are you expecting it to magically guess > > something ?(if so, what?) > > > Fred > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Abbott
2008-Dec-03 22:05 UTC
Re: Am I wrong or is fields_for not working right in Rails 2
Fred, I think you nailed it! Thanks a lot! -- Josh Frederick Cheung wrote:> On Dec 3, 9:50�pm, Joshua Abbott <rails-mailing-l...-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt> > wrote: >> >> to build inputs more like user[billing_address][address_two] >> > Just because lexically it''s ''inside'' the first form isn''t enough (or > to put things another way, it''s hard for code to know about the code > that''s calling them). If you want what you describe you need to be > explicit: use f.fields_for > > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---