# Restful routes, nested resources (and /admin)

**URL:** <https://www.ruby-forum.com/t/restful-routes-nested-resources-and-admin/84235>\
**Category:** Rails\
**Created:** [February 2, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/restful-routes-nested-resources-and-admin/84235 "2007-02-02T17:04:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [February 2, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/restful-routes-nested-resources-and-admin/84235/1 "2007-02-02T17:04:49Z")

</div>

Hello,

I’m now playing with REST and Rails (with Rails 1.2.1).

I have the following routes in my route.rb :

map.with\_options :path\_prefix =\> “/admin”, :name\_prefix =\> “admin\_” do  
|m|  
m.resources :requests, :controller =\> “admin/requests”  
end

This give me a RESTful /admin/requests controller.

In my application, a Request has many Histories.  
How can I add the correct route to have something like that :  
/admin/requests/24/histories.

Without the /admin, it would simply look like this :  
map.resources :requests do |request|  
request.resources :histories  
end

But with the /admin, everything becomes to be confused for me.  
Any help would be greatly appreciated.

Thanks in advance,  
Thomas.

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [February 2, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/restful-routes-nested-resources-and-admin/84235/2 "2007-02-02T18:25:00Z")

</div>

try this:

map.with\_options :path\_prefix =\> “/admin”, :name\_prefix =\> “admin\_” do  
|m|  
m.resources :requests, :controller =\> “admin/requests” do |req|  
req.resources :histories  
end  
end

ed

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [February 2, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/restful-routes-nested-resources-and-admin/84235/3 "2007-02-02T19:56:10Z")

</div>

It seems to work with this in my route.rb :  
map.with\_options :path\_prefix =\> “/admin”, :name\_prefix =\> “admin\_” do  
|m|  
m.resources :requests, :controller =\> “admin/requests” do |request|  
request.resources :histories, :path\_prefix =\>  
“/admin/requests/:request\_id”, :name\_prefix =\> “admin\_”  
end  
end

If I try that in my view :  
\<%= admin\_histories\_url(@request) %\>

I now get this :  
[http://0.0.0.0:3000/admin/requests/24/histories](http://0.0.0.0:3000/admin/requests/24/histories)

If anybody wants to comment this solution, he is more than welcome.  
I’m not sure to do the right thing here.

Thanks,  
Thomas.

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [February 2, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/restful-routes-nested-resources-and-admin/84235/4 "2007-02-02T19:07:05Z")

</div>

Hello Ed,  
Thanks for your answer.

When I do this :  
map.with\_options :path\_prefix =\> “/admin”, :name\_prefix =\> “admin\_” do  
|m|  
m.resources :requests, :controller =\> “admin/requests” do |request|  
request.resources :histories  
end  
end

And in my view I do this :  
\<%= admin\_histories\_url %\>

I get this error :  
undefined local variable or method `admin\_histories\_url’

So I’ve changed to that :  
map.with\_options :path\_prefix =\> “/admin”, :name\_prefix =\> “admin\_” do  
|m|  
m.resources :histories, :controller =\> “admin/histories”  
m.resources :requests, :controller =\> “admin/requests” do |request|  
request.resources :histories  
end  
end

And now : \<%= admin\_histories\_url %\> give me this :  
[http://0.0.0.0:3000/admin/histories](http://0.0.0.0:3000/admin/histories)

If I try this in my view :  
\<%= admin\_histories\_url(@request) %\>

I get this error :  
You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.to\_sym

So I don’t think it solves the problem.  
Any idea?  
Thanks,  
Thomas.

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [February 2, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/restful-routes-nested-resources-and-admin/84235/5 "2007-02-02T20:07:12Z")

</div>

so if you do this:  
\<%= admin\_histories\_url(@request) %\>

you should get something like:  
“/admin/requests/1/histories”

if you do this:  
\<%= admin\_histories\_url(@request, @history) %\>

you should get something like:  
“/admin/requests/1/histories/2”

You have achieved what you requested in your original email, no?

ed

On 2/2/07, Thomas B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > end
> > 
> > ```
> 
> |m|
> 
> > \<%= admin\_histories\_url(@request) %\>
> > 
> > > end
> > > 
> > > > I have the following routes in my route.rb :  
> > > > /admin/requests/24/histories.  
> > > > Thomas.
> 
> >

–  
Ed Hickey  
Developer  
Litmus Media  
816-533-0409  
removed\_email\_address@domain.invalid  
A Member of Think Partnership, Inc  
[www.ThinkPartnership.com](http://www.ThinkPartnership.com)  
Amex ticker symbol: THK

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [February 2, 2007, 8:34pm UTC](https://www.ruby-forum.com/t/restful-routes-nested-resources-and-admin/84235/6 "2007-02-02T20:34:16Z")

</div>

Yes, that’s it.  
But there is still an error in my solution.

Here is the final (I hope) solution :  
map.with\_options :path\_prefix =\> “/admin”, :name\_prefix =\> “admin\_” do  
|m|  
m.resources :requests, :controller =\> “admin/requests” do |request|  
request.resources :histories, :path\_prefix =\>  
“/admin/requests/:request\_id”, :name\_prefix =\> “admin\_”, :controller =\>  
“admin/histories”  
end  
end

In my previous solution, I forgot ":controller =\> “admin/histories”.  
Thanks for your help.

Everybody is still welcome to comment this solution.  
Thomas.

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [February 4, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/restful-routes-nested-resources-and-admin/84235/7 "2007-02-04T15:14:41Z")

</div>

I ran across this the other day and was confused about why the  
with\_options  
options don’t trickle down but I guess it doesn’t actually matter why  
since  
they _don’t_.

RSL
