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?

Apparently this stumped the entire community. I did find (on
http://news.gmane.org/gmane.comp.lang.ruby.rails) that I was not alone
in trying to do something like this, but it appears not to be possible
in the way I was trying to do it. For the benefit of others who may get
stuck on this, here is what I’ve got now:

#MODELS
class User < ActiveRecord::Base
belongs_to :person
end

class Person < ActiveRecord::Base
has_one :user
end

#VIEW
<%= form_tag :action => :signup %>
<%= text_field :user, :username, :size => 40%>

… more :user fields …

<%= text_field :person, :first_name, :size => 40%>

… more :person fields …

<%= submit_tag “Add User” %>
<%= end_form_tag %>

#CONTROLLER

def signup
@user = User.new(params[:user])
@person = @user.build_person(params[:person])
if request.post? && @user.save
flash[:notice] = “User #{@user.username} created.”
redirect_to(:action => “login”)
end
end

IN SUMMARY
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). The code in the controller seems to tie it all up nicely,
although I’ve yet to figure out how to handle correctly failed
validations in the person object. My code will save the user, and
silently not save the person if validation on the person fails.

Ryan

Okay, that last problem was an easy find… adding a call to
validates_associated to the user model as follows does the trick:

class User < ActiveRecord::Base
belongs_to :person
validates_associated :person
end

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?

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

It looks like that’s what you need.

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/

Michael S. 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

ActionView::Helpers::FormHelper

It looks like that’s what you need.

Michael

Very 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