# 3 models in 1 template

**URL:** <https://www.ruby-forum.com/t/3-models-in-1-template/92069>\
**Category:** Rails\
**Created:** [April 15, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/3-models-in-1-template/92069 "2007-04-15T17:51:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [April 15, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/3-models-in-1-template/92069/1 "2007-04-15T17:51:15Z")

</div>

hello,

I’d like to get user data in one template.

The relations are:  
user has\_one user\_adress  
user has\_one user\_account

The following template doesn’t save any (valid) data. I think the  
submit button doesn’t work.  
All 3 models should be validated when save is called.

\<%= error\_messages\_for ‘user’ %\>  
\<%= error\_messages\_for ‘user\_adress’ %\>  
\<%= error\_messages\_for ‘user\_account’ %\>

 Please enter your data: 

```
<% form_for :user do |form| %>
    <label for="user_first_name">firstname:</label>
    <%= form.text_field :first_name, :size => 40 %>
  </p>
  <p>
    <label for="user_surname">surname:</label>
    <%= form.text_field :surname, :size => 40 %>
  </p>
  <p>
    <label for="user_birthdate">date of birth</label><br/>
    <%= date_select 'user', 'birthdate', :include_blank => true,
    :order => [:day, :month, :year], :start_year => 1900, :default

```

=\> :blank %\>

  

email:  
\<%= form.text\_field :email, :size =\> 40 %\>

  

username:  
\<%= form.text\_field :username, :size =\> 40 %\>

  

password:  
\<%= form.password\_field :password, :size =\> 40 %\>

  

passwort  
confirmation:  
\<%= form.password\_field :password\_confirmation, :size =\> 40 %\>

  
\<% end %\>   

```
<legend>Your adress:</legend>
<% form_for :user_adress do |form| %>
  <p>
    <label for="user_adress_street">street:</label>
    <%= form.text_field :street, :size => 40 %>
  </p>
  <p>
    <label for="user_adress_nr">number:</label>
    <%= form.text_field :nr, :size => 40 %>
  </p>
  <p>
    <label for="user_adress_postcode">postcode:</label>
    <%= form.text_field :postcode, :size => 40 %>
  </p>
  <p>
    <label for="user_adress_city">City:</label>
    <%= form.text_field :city, :size => 40 %>
  </p>
<% end %>

```

  

```
<legend>Your payment details:</legend>
<% form_for :user_account do |form| %>
  <p>
    <label for="user_account_bank">credit institution:</label>
    <%= form.text_field :bank, :size => 40 %>
  </p>
  <p>
    <label for="user_account_bank_code">bank code:</label>
    <%= form.text_field :bank_code, :size => 40 %>
  </p>
  <p>
    <label for="user_account_account_number">account number:</

```

label\>  
\<%= form.text\_field :account\_number, :size =\> 40 %\>

  
\<% end %\>   

\<%= submit\_tag “Register”, :class =\> “submit” %\>

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [April 15, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/3-models-in-1-template/92069/2 "2007-04-15T18:20:42Z")

</div>

This creates 3 forms, but no submit tag in any of them.

Try using this:

form\_for :user, @user, :url =\> {:action =\> :new} do |form|  
#…your fields for user

fields\_for :user\_address, @user\_address do |fields|  
#…your fields for user\_adress in the following form:  
# fields.textarea (arguments)  
end

fields\_for :user\_account, @user\_account do |fields|  
#same as for user\_adress  
end

\<%= submit\_tag “Register”, :class =\> “submit” %\>  
end

:user is the class of the object of interest  
@user is an actual instance (which could have another name like  
@new\_user)  
:url points the form to it’s target

same for the fields\_for, but subforms do not get a target.

Have Fun  
Skade

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [April 16, 2007, 1:20am UTC](https://www.ruby-forum.com/t/3-models-in-1-template/92069/3 "2007-04-16T01:20:23Z")

</div>

there’s also a problem in my controller. Neither does it save anything  
to the database nor validate the models.

The template is register.rhtml. I wrote it as you said.

def register  
@user = User.new  
end

def new  
if @user.save  
@user.adress = @user\_adress  
@user.account = @user\_account  
flash[:notice] = ‘Registration was successful.’  
redirect\_to :action =\> ‘index’  
else  
render :action =\> ‘register’  
end  
end

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [April 16, 2007, 1:33am UTC](https://www.ruby-forum.com/t/3-models-in-1-template/92069/4 "2007-04-16T01:33:23Z")

</div>

I forgot to say that the relations are dependent.

And not everyone does have an account. So, if there’s nothing in the  
account field, the account shouldn’t be saved.

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [April 16, 2007, 1:35am UTC](https://www.ruby-forum.com/t/3-models-in-1-template/92069/5 "2007-04-16T01:35:33Z")

</div>

Martin Luy wrote:

> there’s also a problem in my controller. Neither does it save anything  
> to the database nor validate the models.
> 
> The template is register.rhtml. I wrote it as you said.
> 
> def register  
> @user = User.new  
> end
> 
> def new  
> if @user.save  
> @user.adress = @user\_adress  
> @user.account = @user\_account  
> flash[:notice] = ‘Registration was successful.’  
> redirect\_to :action =\> ‘index’  
> else  
> render :action =\> ‘register’  
> end  
> end

Please consider that all Rails operations are stateless. You need to  
instantiate a new User in def new. You will find the contents of the  
form via params(:user).

Try:

def new  
@user = User.new(params(:user))  
@user.address.build(params(:user\_adress))  
@user.account.build(params(:user\_account))  
if @user.save!  
flash[:notice] = ‘Registration was successful.’  
redirect\_to :action =\> ‘index’  
else  
render :action =\> ‘register’  
end  
end

I hope, you get the point, i can’t check the code right now.

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [April 16, 2007, 1:38am UTC](https://www.ruby-forum.com/t/3-models-in-1-template/92069/6 "2007-04-16T01:38:22Z")

</div>

Martin Luy wrote:

> I forgot to say that the relations are dependent.
> 
> And not everyone does have an account. So, if there’s nothing in the  
> account field, the account shouldn’t be saved.

class User \< ActiveRecord::Base  
validates\_associated :account, :address  
end

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [April 17, 2007, 12:22am UTC](https://www.ruby-forum.com/t/3-models-in-1-template/92069/7 "2007-04-17T00:22:54Z")

</div>

Thanks for your help, I got to the following working code. 2 more  
questions after the code.

In the controller:

def register  
@user = User.new(params[:user])  
@user.time\_of\_registration = Time.now  
@user.user\_adress = UserAdress.new(params[:user\_adress])  
@user.user\_account = UserAccount.new(params[:user\_account])  
if request.post? and @user.save  
flash[:notice] = ‘Registration was successful.’  
redirect\_to :action =\> ‘login’  
end  
end

register.rhtml:

\<%= error\_messages\_for ‘user’ %\>

 Please enter your data: 

```
<% form_for :user, @user do |form| %>
  <p>
    <label for="user_first_name">First name:</label>
    <%= form.text_field :first_name, :size => 40 %>
  </p>
  ...

  <% fields_for :user_adress, @user_adress do |fields| %>
    ...
  <% end %>

  <p>If you want to pay using debit, please enter your bank

```

account:

  
\<% fields\_for :user\_account, @user\_account do |fields| %\>  

credit institution:  
\<%= fields.text\_field :bank, :size =\> 40 %\>

  

bank code:  
\<%= fields.text\_field :bank\_code, :size =\> 40 %\>

  

account number:\</  
label\>  
\<%= fields.text\_field :account\_number, :size =\> 40 %\>

  
\<% end %\>

```
  <%= submit_tag "Register", :class => "submit" %>

<% end %>

```

The user model:

class User \< ActiveRecord::Base

has\_one :user\_adress, :dependent =\> true  
has\_one :user\_account, :dependent =\> true

validates\_associated :user\_account, :user\_adress  
…  
end

The account model:

class UserAccount \< ActiveRecord::Base  
belongs\_to :user

validates\_presence\_of :bank, :bank\_code, :account\_number

validates\_numericality\_of …  
validates\_inclusion\_of …  
validates\_format\_of …  
end

Questions:

Validation of user\_account and user\_adress looks like “User adress is  
invalid”. I would like to show the errors for the children classes  
explicitly.

How can I change “validate\_presence\_of” in UserAccount so that either  
all or no fields are entered?

Thanks,  
Luma
