Ryan Sandridge
2006-Aug-11 12:27 UTC
[Rails] Accessing belongs_to objects from a form_for context
In a form_for context, is there a way to access objects that are related to the primary object with a belongs_to? I think an example will serve best: class User < ActiveRecord::Base belongs_to :person attr_accessor :username end class Person < ActiveRecord::Base has_one :user attr_accessor :first_name end <% form_for :user do |form| %> <%= form.text_field :username, :size => 40%> <%= form.text_field "person.first_name", :size => 40%> ????? doesn''t work <%= submit_tag "Add User", :class => "submit" %> <% end %> Is there a way to do this? -- Posted via http://www.ruby-forum.com/.
MIke Cahill
2006-Aug-12 02:03 UTC
[Rails] Re: Accessing belongs_to objects from a form_for context
Still trying to figure out similar issues myself... did you try "person[first_name]" ? Ryan Sandridge wrote:> In a form_for context, is there a way to access objects that are related > to the primary object with a belongs_to? I think an example will serve > best: > > class User < ActiveRecord::Base > belongs_to :person > attr_accessor :username > end > > class Person < ActiveRecord::Base > has_one :user > attr_accessor :first_name > end > > > <% form_for :user do |form| %> > <%= form.text_field :username, :size => 40%> > <%= form.text_field "person.first_name", :size => 40%> ????? > doesn''t work > <%= submit_tag "Add User", :class => "submit" %> > <% end %> > > > Is there a way to do this?-- Posted via http://www.ruby-forum.com/.
Ryan Sandridge
2006-Aug-23 01:00 UTC
[Rails] Re: Accessing belongs_to objects from a form_for context
Michael Schuerig wrote:> On Tuesday 22 August 2006 05:16, Ryan Sandridge wrote: >> I learned that I shouldn''t have the attr_accessor''s for database >> backed attributes in the model. I learned that there is currently no >> way to access child objects through the form_for mechanism (use >> form_tag instead). > > Have look at the docs for fields_for > > http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M000388 > > It looks like that''s what you need. > > MichaelVery nice. It does indeed look like it could do the trick. Considering how often I saw this question asked on other rails forums, I''m shocked to just now see this answer. Thanks much, Ryan -- 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 -~----------~----~----~----~------~----~------~--~---