# Accessing Member Object Methods

**URL:** <https://www.ruby-forum.com/t/accessing-member-object-methods/156124>\
**Category:** Rails\
**Created:** [December 21, 2008, 1:08am UTC](https://www.ruby-forum.com/t/accessing-member-object-methods/156124 "2008-12-21T01:08:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![idflyfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@idflyfish](https://www.ruby-forum.com/u/idflyfish)\
**Post date:** [December 21, 2008, 1:08am UTC](https://www.ruby-forum.com/t/accessing-member-object-methods/156124/1 "2008-12-21T01:08:39Z")

</div>

Hello,

```
 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?

Thanks!

---

<div class="post-metadata">

**Author:** ![idflyfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@idflyfish](https://www.ruby-forum.com/u/idflyfish)\
**Post date:** [December 21, 2008, 5:51am UTC](https://www.ruby-forum.com/t/accessing-member-object-methods/156124/2 "2008-12-21T05:51:37Z")

</div>

At your controller:

class UsersController \< ApplicationController

```
def new
    @user = User.new
    @contact = @user.build_contact
end

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

```

end

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 %\>

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![idflyfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@idflyfish](https://www.ruby-forum.com/u/idflyfish)\
**Post date:** [December 22, 2008, 5:37am UTC](https://www.ruby-forum.com/t/accessing-member-object-methods/156124/3 "2008-12-22T05:37:15Z")

</div>

On Dec 20, 11:50 pm, “Maurício Linhares” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:  
[…]

> ```
> <% end %>
> 
> ```

Um, those last two should be g.text\_field .

> \<% end %\>

See

> **[Boost your Rails Controller params\[:fu\]](http://www.stephenchu.com/2008/03/boost-your-controller-paramsfu.html)**
>
> In many ways, in a Rails controller POST action, what gets POST-ed in params are often overlooked. In many straight-forward tutorials, we s...

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

## Best,

Marnen Laibow-Koser  
removed\_email\_address@domain.invalid  
[http://www.marnen.org](http://www.marnen.org)
