# :path\_prefix on resources not passing to actions?

**URL:** <https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290>\
**Category:** Rails\
**Created:** [July 28, 2008, 10:00am UTC](https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290 "2008-07-28T10:00:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [July 28, 2008, 10:00am UTC](https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290/1 "2008-07-28T10:00:30Z")

</div>

I am experiencing some difficulties using :path\_prefix on resources.

I do the following  
$ rails pathtest  
$ cd pathtest  
$ script/generate scaffold thing title:string  
$ rake db:migrate

Then in routes.rb I change:  
map.resources :things  
to  
map.resources :things, :path\_prefix =\> ‘:blah’

And doing a rake routes I get the following:  
things GET /:blah/things  
{:action=\>“index”, :controller=\>“things”}  
formatted\_things GET /:blah/things.:format  
{:action=\>“index”, :controller=\>“things”}  
POST /:blah/things  
{:action=\>“create”, :controller=\>“things”}  
POST /:blah/things.:format  
{:action=\>“create”, :controller=\>“things”}  
new\_thing GET /:blah/things/new  
{:action=\>“new”, :controller=\>“things”}  
formatted\_new\_thing GET /:blah/things/new.:format  
{:action=\>“new”, :controller=\>“things”}  
edit\_thing GET /:blah/things/:id/edit  
{:action=\>“edit”, :controller=\>“things”}  
formatted\_edit\_thing GET /:blah/things/:id/edit.:format  
{:action=\>“edit”, :controller=\>“things”}  
thing GET /:blah/things/:id  
{:action=\>“show”, :controller=\>“things”}  
formatted\_thing GET /:blah/things/:id.:format  
{:action=\>“show”, :controller=\>“things”}  
PUT /:blah/things/:id  
{:action=\>“update”, :controller=\>“things”}  
PUT /:blah/things/:id.:format  
{:action=\>“update”, :controller=\>“things”}  
DELETE /:blah/things/:id  
{:action=\>“destroy”, :controller=\>“things”}  
DELETE /:blah/things/:id.:format  
{:action=\>“destroy”, :controller=\>“things”}  
/:controller/:action/:id  
/:controller/:action/:id.:format

But…  
got to localhost:3000/my/things works fine. I create a new thing.  
Then I get this error:  
thing\_url failed to generate from {:action=\>“show”, :blah=\>#\<Thing id:  
1, title: “test”, created\_at: “2008-07-28 07:55:27”, updated\_at:  
“2008-07-28 07:55:27”\>, :controller=\>“things”}, expected:  
{:action=\>“show”, :controller=\>“things”}, diff: {:blah=\>#\<Thing id: 1,  
title: “test”, created\_at: “2008-07-28 07:55:27”, updated\_at:  
“2008-07-28 07:55:27”\>}

And I can no longer go to /my/things because of that error.  
It seems that when it is trying to create  
(eval):17:in `thing\_path’

the :blah value is not being set.  
I have to edit all my views to do this:

```
<td><%= link_to 'Show', thing_path(params[:blah], thing) %></td>

```

instead of

\<%= link\_to ‘Show’, thing %\>

I think this is a bug in rails… or am I misunderstanding how this  
should work?

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [July 28, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290/2 "2008-07-28T14:15:58Z")

</div>

Hi –

On Mon, 28 Jul 2008, phil wrote:

> map.resources :things  
> to  
> map.resources :things, :path\_prefix =\> ‘:blah’
> 
> And doing a rake routes I get the following:  
> [snip]
> 
> I think this is a bug in rails… or am I misunderstanding how this  
> should work?

You’re misunderstanding how it should work. It’s certainly not a bug  
in Rails; the routing system may have bugs, but not at this basic  
level.

The problem, as it seems to be so often these days, is that you’re  
using the scaffolding and then being surprised when it turns out you  
actually have to write code. The scaffolding is just a demo.

I don’t mean to pounce on you about this. There’s a lot of scaffolding  
around these days, it seems, and therefore a lot of questions about  
how to solve the problems it creates. The best thing to do is to  
forget the scaffolding and study the routing system, and the other  
systems in Rails, directly.

Anyway – if you’ve got a resource route that looks like this:

/:blah/things/:id

then with the GET request method it will go to the show action. But it  
needs some way to fill in everything in the route string. It tries to  
do this from the arguments to the named route. The first thing it does  
is fill in the :blah field. If the only argument to the named route is  
a Thing object, then it assigns that object to :blah – and then it’s  
run out of arguments, so it can’t figure out what :id is supposed to  
be.

(The error messages on this stuff are a little hard to decipher, since  
they might lead you to believe that it doesn’t want a :blah value. But  
it does; it just doesn’t have enough to do the whole path.)

That’s why you need two arguments:

thing\_path(“my”, @thing)

or equivalent.

David

–  
Rails training from David A. Black and Ruby Power and Light:

- Advancing With Rails August 18-21 Edison, NJ
- Co-taught by D.A. Black and Erik Kastner  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [July 28, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290/3 "2008-07-28T14:28:53Z")

</div>

Ok…fair enough. I guess I didn’t expect when I added a path\_prefix  
to have to change every link in my code!

I guess I am surprised because it does pass through the :blah value to  
certain links, but not to the resource links. For instance on the  
(yes, scaffold generated!) pages the links to /things will  
automatically be updated to have my/things. It is this inconsistent  
behaviour that I am questioning - the fact that the plurals calls DO  
update and the singular calls don’t.

For example:  
\<%= link\_to ‘Back’, things\_path %\>

On the page /my/things/new  
Will generate a link like this:  
[http://localhost:3000/my/things](http://localhost:3000/my/things)

and indeed the form action will be:

So the problem seems to be a) in singular routes and b) with  
path\_prefixes with variables because if your routes looks like:  
map.resources :things, :path\_prefix =\> “my”

Then everything works a treat.

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [July 28, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290/4 "2008-07-28T17:06:00Z")

</div>

Ok - you don’t HAVE to be condescending you know!

I am familiar with routing, it is this particular area (path\_prefix) I  
find to be completely undocumented as to how it works (I sense another  
condescending post from you telling me its not). I appreciate that you  
are trying to post a lot to look knowledgeable so that people can pay  
a lot of money to have you read Agile Web D. With Rails to  
them.

Anyway, I have discovered in the code what I was looking for.

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [July 28, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290/5 "2008-07-28T14:45:53Z")

</div>

Hi –

On Mon, 28 Jul 2008, phil wrote:

> > > $ script/generate scaffold thing title:string  
> > > But…  
> > > It seems that when it is trying to create  
> > > should work?  
> > > around these days, it seems, and therefore a lot of questions about  
> > > do this from the arguments to the named route. The first thing it does
> 
> automatically be updated to have my/things. It is this inconsistent  
> and indeed the form action will be:
> 
> So the problem seems to be a) in singular routes and b) with  
> path\_prefixes with variables because if your routes looks like:  
> map.resources :things, :path\_prefix =\> “my”
> 
> Then everything works a treat.

The behavior isn’t inconsistent with the documented rules of the  
routing system. The plural one – the “Back” link that shows  
“/my/things” – uses the method things\_path. That method also requires  
a :blah value. It defaults to the existing :blah value. The reason  
that doesn’t work with the other ones is because of the following  
rule: Given a named route like this:

```
something_path ':x/:y/:z'

```

x, y, and z will default to whatever they were on the last response,  
_unless_ you override any one of them. If you override any of them,  
then that one _and all to the right of it_ will not default.

things\_path doesn’t override anything, so it takes the :blah value  
from the last response (“my”). However, thing\_path(@thing) (or  
equivalent) _does_ override the :blah value, since it assumes that its  
first argument is meant to populate the :blah field. So all bets are  
off.

By the same token, if you do this in the index view:

 \<%= link\_to 'Show', thing\_path(:id =\> thing) %\>

you’ll find that :blah defaults to “my” (or whatever it was coming  
in). That’s because you’re explicitly using thing for the :id field  
(which, by the way, it will populate by calling the method to\_params  
on thing). Since the :id field is to the right of the :blah field in  
the route string (“:blah/things/:id”), it fills in the :blah field  
with the existing value.

As I said, the best thing is to actually study the routing system 🙂  
It all really does make sense, and at some point you’re going to need  
to know how it thinks in order both to solve problems and to get the  
most out of it.

David

–  
Rails training from David A. Black and Ruby Power and Light:

- Advancing With Rails August 18-21 Edison, NJ
- Co-taught by D.A. Black and Erik Kastner  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [July 28, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290/6 "2008-07-28T17:52:56Z")

</div>

Uh, I don’t think he was being condescending. I think he was being  
really helpful. I’m not sure why you’re being so defensive about  
someone answering your rather entry level question.

It’s entirely likely it is undocumented. But other people apparently  
understand how it works, so it’s apparently not so obtuse that it  
can’t be understood. Perhaps reading the code wouldn’t cause you too  
much pain? People do add inline comments sometimes. Or maybe reading  
a book? According to O’Reilly Safari, it’s documented in no less than  
three books, including David’s eBook on routing (of course, reading  
that would be one protracted condescension, though, right?). And  
those are just the ones that I could find by doing a simple search.

Even so, if you’re “familiar with routing,” path\_prefix works EXACTLY  
like any other wildcard value. I find it VERY hard to believe that  
you’re “familiar” routing and don’t get that wildcard values require  
value in route generation. If you had something like:

```
map.thing ":a/:b/:c"

```

You couldn’t expect to do “thing\_path” and have it generate a URL.

Next time you decide to be a prick, please do it someone else other  
than someone who’s helping you (and has spent a TON of time helping  
others).

–Jeremy

On Mon, Jul 28, 2008 at 10:05 AM, phil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anyway, I have discovered in the code what I was looking for.
> 
> >

–  
[http://jeremymcanally.com/](http://jeremymcanally.com/)  
[http://entp.com/](http://entp.com/)

> **[Omg GIFs | GIFDB.com](https://gifdb.com/omg)**
>
> Download Omg GIFs for Free on GifDB. More than 56 Omg Animated GIFs to download.

My books:

> **[Ruby in Practice](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...

[http://humblelittlerubybook.com/](http://humblelittlerubybook.com/) (FREE!)

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [July 28, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290/7 "2008-07-28T18:45:05Z")

</div>

David,

I really admire your professionalism. That was quite an underhanded  
character snipe but you handled it gracefully.

phil,

Shame on you. David is one of the nicest and most knowledgeable people  
you will meet in the Ruby community. I think he was being helpful  
above and beyond the call without even a hint of condescension, taking  
his time to give you a careful and complete answer. How dare you  
attack his professional ethics! Not only are you potentially liable  
for slander for what you said, it’s just plain childish and hateful.  
You’re lucky that David is a nice guy or you might very well have  
gotten sued.

I hope that everyone who comes here to ask for help (which is  
everyone) will keep in mind that the people that help you are doing so  
for your benefit, and that how you treat them will determine the  
amount of help you (and everyone else) will receive from them in the  
future. There is never, ever any call for this sort of behavior.

I hope that everyone who comes here to offer help will keep in mind  
that most people are not as rude and ungrateful as phil. We do  
appreciate your help!

Remember: We Are Nice Because Matz Is Nice. Be nice!

Best,

Rein H.  
Hashrocket  
[reinh.com](http://reinh.com)

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [July 30, 2008, 12:38am UTC](https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290/8 "2008-07-30T00:38:24Z")

</div>

You’re both right, I apologize.  
I personally don’t think David’s email was that helpful - or that the  
question was ‘entry level’. But c’est la vie. I should not have  
reacted so, and you are right to call me on it.

FWIW, I have made an interesting change to the way polymorphic\_path  
works that actually lets rails behave as I thought it should (by  
passing those path\_prefix’s down to the singular resources below).  
I’ve seen a few postings on here and other places about people  
suffering the same problems as I.

So - it all seems to have worked out in the end.

Again, sorry for the bruised ego’s.

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [July 28, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/path-prefix-on-resources-not-passing-to-actions/144290/9 "2008-07-28T18:09:20Z")

</div>

Hi –

On Mon, 28 Jul 2008, phil wrote:

> Ok - you don’t HAVE to be condescending you know!
> 
> I am familiar with routing, it is this particular area (path\_prefix) I  
> find to be completely undocumented as to how it works (I sense another  
> condescending post from you telling me its not). I appreciate that you  
> are trying to post a lot to look knowledgeable so that people can pay  
> a lot of money to have you read Agile Web D. With Rails to  
> them.

Please don’t publicly attack my classroom skills, professionalism, or  
ethics, all of which are demonstrably quite sound.

I answer questions on mailing lists and other forums principally for  
two reasons: to help people (and I was doing it regularly, fifteen  
years before I started making my living from training and consulting),  
and to educate myself. I’ve learned a ton by tracking down answers and  
puzzling things out away from the list.

Just so you know: in order to help you with your question, I ran the  
commands you ran (creating the ‘pathtest’ Rails app and the  
scaffolding), migrated the database, opened the console and created a  
record, started the server, checked to see that I was getting the same  
results as you were, and went back in the console and did things like  
this for a while:

> > app.thing\_path(:blah =\> “abc”)  
> > app.thing\_path(“abc”)  
> > app.thing\_path(“abc”, “def”)  
> > app.thing\_path(“my”, Thing.find(:first))

to make sure I understood what was going on and what was not working  
and why. Then I made manual changes to the scaffold views, so that I  
could verify that what I expected to happen would actually happen, and  
make sure that the advice and explanations I was planning to give you  
would be correct.

Then I spent about 15 minutes composing an answer to you.

None of this guarantees that my response will have helped you (though  
if you think these things are under-documented, I would have thought  
that getting an explanation would in fact have been helpful). But  
believe me, if I were going to condescend to you, I wouldn’t set aside  
a large chunk of my morning to do it.

Now let’s get back to technical matters.

David

–  
Rails training from David A. Black and Ruby Power and Light:

- Advancing With Rails August 18-21 Edison, NJ
- Co-taught by D.A. Black and Erik Kastner  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
