Accessing Member Object Methods


     I am hoping one of you Rails gurus can help a poor newb.

     I have a User model and a Contact model. The user Model
has_one :contact. I would like to use the users/new view to create a
new user as well as the related contact. However I can't figure out
how to add the appropriate text_fields to the form to about info for
the related contact. Can anyone point me in the right direction?


At your controller:

class UsersController < ApplicationController

    def new
        @user =
        @contact = @user.build_contact

    def create
        @user = params[:user] )
        @contact = @user.build_contact( params[:contact] )
        if @user.valid? and @contact.valid?
            respond_to do |format|
                format.html do
                    flash[:notice] = "User saved"
                    redirect_to users_path
            render :action => 'new'


At your new.html.erb

<% form_for @user do |f| %>

    Login: <%= f.text_field :login %> <br/>
    Email: <%= f.text_field :email %> <br/>

    <% fields_for :contact do |g| %>
       Name: <%= text_field :email %>
       Address: <%= text_area :address %>
    <% end %>

<% end %>


At your new.html.erb

<% form_for @user do |f| %>

Login: &lt;%= f\.text\_field :login %&gt; &lt;br/&gt;
Email: &lt;%= f\.text\_field :email %&gt; &lt;br/&gt;

&lt;% fields\_for :contact do |g| %&gt;
   Name: &lt;%= text\_field :email %&gt;
   Address: &lt;%= text\_area :address %&gt;
&lt;% end %&gt;

Um, those last two should be g.text_field .

<% end %>

for an excellent series of blog posts detailing this and other similar