# Understanding path\_prefix in restful routes

**URL:** <https://www.ruby-forum.com/t/understanding-path-prefix-in-restful-routes/106437>\
**Category:** Rails\
**Created:** [June 10, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/understanding-path-prefix-in-restful-routes/106437 "2007-06-10T15:48:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [June 10, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/understanding-path-prefix-in-restful-routes/106437/1 "2007-06-10T15:48:25Z")

</div>

Hi all,

I have a hard time understanding path\_prefix in restful routes.

My urls should always start with the id of a club since my site is  
club oriented eg: [www.clubsite.com/zero/](http://www.clubsite.com/zero/) where zero is the club

I have  
map.resources :clubs  
map.resources :users, :path\_prefix =\> ‘/:club’  
map.resources :posts, :path\_prefix =\> ‘/:club’

I don’t see how rails knows what :club is. Do I have to set it  
somewhere (in my application controller)?

Concrete I have the following problem:  
But when I use \<%= link\_to “new post”, post\_url(1) %\>  
I get an error post\_url failed to generate from  
{:club=\>“1”, :controller=\>“posts”, :action=\>“show”}, expected:  
{:controller=\>“posts”, :action=\>“show”}, diff: {:club=\>“1”}

It seems that :club = 1 when it actualy should be a string (zero)

Many thanks in advance  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [June 11, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/understanding-path-prefix-in-restful-routes/106437/2 "2007-06-11T21:31:24Z")

</div>

When you nest resources, the route helpers that are created for that  
resource will expect additional arguments to match the named  
parameters provided in the :path\_prefix option. So, for example, the  
route helper for ClubsController#new would just be new\_club\_path, but  
the route helper for PostsController#new would be new\_club\_path(:club  
=\> X) or just new\_club\_path(X), where X is the parent Club or the  
parent Club’s ID.

In your example, it looks like you might be using the wrong helper.  
Your link\_to label is “new post” but the route helper you are using is  
for PostsController#show. What you probably want is this: \<%= link\_to  
“new post”, new\_post\_path(1) %\>. (The show helper [post\_path] will  
require two arguments: the parent Club and the existing Post to be  
shown. It would be called as post\_path(:club =\> X, :id =\> Y) or just  
post\_path(X, Y).)

Note that this just covers how the Club ID gets into the URL and  
params[:club]. If you want to work with the actual club from within  
any of the actions in UsersController or PostsController, those  
actions will need to use params[:club] to look up the actual Club  
instance, e.g., Club.find(params[:club]).

-JP
