# Rails routing

**URL:** <https://www.ruby-forum.com/t/rails-routing/163577>\
**Category:** Rails\
**Created:** [March 22, 2009, 11:16am UTC](https://www.ruby-forum.com/t/rails-routing/163577 "2009-03-22T11:16:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [March 22, 2009, 11:16am UTC](https://www.ruby-forum.com/t/rails-routing/163577/1 "2009-03-22T11:16:29Z")

</div>

Hi, everybody

Is there a way to simplify address path from:  
[http://0.0.0.0:3000/books/3/pages/1](http://0.0.0.0:3000/books/3/pages/1)  
to this format:  
[http://0.0.0.0:3000/3/1](http://0.0.0.0:3000/3/1)

My current routes.rb looks like:

ActionController::Routing::Routes.draw do |map|  
map.resources :books do |book|  
book.resources :pages  
end  
end

Many thanks for your help,

Pulu

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [March 22, 2009, 1:39pm UTC](https://www.ruby-forum.com/t/rails-routing/163577/2 "2009-03-22T13:39:27Z")

</div>

On 22 Mar., 11:16, “Paul A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> map.resources :books do |book|  
> book.resources :pages  
> end  
> end
> 
> Many thanks for your help,

map.short\_book\_page “:book\_id/:id”, :book\_page

It’s a named route, so you can call it like this: link\_to “Page 2,  
book 1”, short\_book\_page\_path(1, 2)

It’s untested but it should work. Take a look at the documentation for  
ActionController::Routing at  
[http://api.rubyonrails.org/classes/ActionController/Routing.html](http://api.rubyonrails.org/classes/ActionController/Routing.html)

–  
Best regards,  
David K.  
[http://twitter.com/rubyguy](http://twitter.com/rubyguy)

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [March 23, 2009, 7:58am UTC](https://www.ruby-forum.com/t/rails-routing/163577/3 "2009-03-23T07:58:10Z")

</div>

I would write the thing you’re trying to do as:

map.short\_book\_page “:book\_id/:page\_id”, :controller =\> “books”,  
:action =\>  
“my\_action”

However…

Defining your route in this way will have the probably unintended effect  
of  
shortcircuiting your default routes (i.e. “:controller/:action”). If  
that  
doesn’t matter to you, then stick with the simplest option. But you can  
also put a condition on the parameters like this:

map.short\_book\_page “:book\_id/:page\_id”, :controller =\> “books”,  
:action =\>  
“my\_action”, :book\_id =\> /\d+/, :page\_id =\> /\d+/

This should now only match in situations where you’ve got numeric  
parameters passed for both book\_id and page\_id.

However…

You should understand that by doing it this way you’re draining your  
URLs of  
any semantic meaning and search engines will probably not know what to  
make  
of them. Best practice is generally to provide as many hints in your  
anchors as to what’s being linked to, if not in the link text itself  
then at  
least in the URLs. So unless you’re building an API and you don’t care  
about Google juice, I’d stick with the more traditional way of doing  
things.
