# Routing to a new action of a nested resource

**URL:** <https://www.ruby-forum.com/t/routing-to-a-new-action-of-a-nested-resource/152468>\
**Category:** Rails\
**Created:** [November 4, 2008, 10:58am UTC](https://www.ruby-forum.com/t/routing-to-a-new-action-of-a-nested-resource/152468 "2008-11-04T10:58:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [November 4, 2008, 10:58am UTC](https://www.ruby-forum.com/t/routing-to-a-new-action-of-a-nested-resource/152468/1 "2008-11-04T10:58:09Z")

</div>

Hi, can anyone help with a routing problem for nested resources:

My models are User (has\_one :house) and House (belongs\_to :user).

I have a houses\_controller and a user\_house\_controller. house\_controller  
is for the admin to perform CRUD on houses; so users’ CRUD must go  
through the user\_house\_controller.

The resources are nested like so:

map.resources :houses

map.resources :users do |users|  
users.resource :house, :controller =\> ‘user\_house’  
end

So after a new user registers I want to send him to the form for the his  
house, ie

[http://localhost:3000/users/21/gym/new](http://localhost:3000/users/21/gym/new)

I’m having trouble with generating this URL programmatically. In my  
users\_controller I have:

def create  
@user = User.new(params[:user]) #user registered, now register his  
house  
redirect\_to new\_gym\_url(:user\_id =\> @user)

I know when one has a nested resource then the URL generator takes 2  
arguments: The nested-under model and the nested model. So according to  
this logic, the URL helper should look like:

new\_gym\_url(:user\_id =\> @user, :id =\> house)

But then I will be referring to a house that hasn’t been created yet!  
When I just use:

new\_gym\_url(:user\_id =\> @user) or new\_gym\_url

I get redirected to the houses\_controller#new, and I want to use  
user\_house\_controller.

This is about to drive me nuts; so please explain!

/ Vahagn

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [November 4, 2008, 11:04am UTC](https://www.ruby-forum.com/t/routing-to-a-new-action-of-a-nested-resource/152468/2 "2008-11-04T11:04:39Z")

</div>

PS! Correct url is [http://localhost:3000/users/21/house/new](http://localhost:3000/users/21/house/new)

🙂

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [November 4, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/routing-to-a-new-action-of-a-nested-resource/152468/3 "2008-11-04T14:08:12Z")

</div>

Issue solved!

users.resource :houses, :controller =\> ‘user\_house’

instead of:

users.resource :house, :controller =\> ‘user\_house’  
end

Apparently the resources are always plural, regardless that a user only  
has one house.

/V.

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [November 24, 2008, 3:52am UTC](https://www.ruby-forum.com/t/routing-to-a-new-action-of-a-nested-resource/152468/4 "2008-11-24T03:52:51Z")

</div>

On Nov 4, 8:08 am, Vahagn H. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> has one house.
> 
> /V.

Hi Vahagn. Resources don’t have to be plural. For example, one of my  
apps has users, and when they login, they access their account at this  
path:  
/account/  
To do this, I added the following to routes.rb:  
map.resource :account  
You just need to make sure that all of your files and classes have  
proper pluralisation. Thus, I have AccountsController.

For your question, I would think that you’d need:

# config/routes.rb

map.resources :users do |users|  
users.resource :house, :controller =\> ‘user\_house’  
end

# app/controllers/houses\_controller.rb

class HousesController \< ApplicationController

In other words, make sure it’s “HousesController” and  
“houses\_controller.rb” (plural), not “HouseController” or  
“house\_controller.rb” (singular).

Once you have that working properly, you can generate URLs and paths  
to a user’s house like this:  
user\_house\_path  
user\_house\_url

I hope that helps,  
Nick

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [November 25, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/routing-to-a-new-action-of-a-nested-resource/152468/5 "2008-11-25T18:07:36Z")

</div>

Hi Nick,-

thanks for your comment, I appreciate it. I’ve worked around the problem  
but it is definitely helpful. Previously I thought that pluralization  
patterns in the routes file must follow those of the controllers but now  
I can see it’s not the case - the two things are decoupled and the most  
important thing is the proper pluralization of the controllers.

All the best,  
Vahagn

Nick H. wrote:

> On Nov 4, 8:08ï¿½am, Vahagn H. \<rails-mailing-l…@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > has one house.
> > 
> > /V.
> 
> Hi Vahagn. Resources don’t have to be plural. For example, one of my  
> apps has users, and when they login, they access their account at this  
> path:  
> /account/  
> To do this, I added the following to routes.rb:  
> map.resource :account  
> You just need to make sure that all of your files and classes have  
> proper pluralisation. Thus, I have AccountsController.
> 
> For your question, I would think that you’d need:
> 
> # config/routes.rb
> 
> map.resources :users do |users|  
> users.resource :house, :controller =\> ‘user\_house’  
> end
> 
> # app/controllers/houses\_controller.rb
> 
> class HousesController \< ApplicationController
> 
> In other words, make sure it’s “HousesController” and  
> “houses\_controller.rb” (plural), not “HouseController” or  
> “house\_controller.rb” (singular).
> 
> Once you have that working properly, you can generate URLs and paths  
> to a user’s house like this:  
> user\_house\_path  
> user\_house\_url
> 
> I hope that helps,  
> Nick
