Forum: Ruby on Rails Rails routing

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C388f6c1fd2bf8c651678e14b78aefa7?d=identicon&s=25 Paul A. (pulu)
on 2009-03-22 11:16
Hi, everybody

Is there a way to simplify address path from:
  http://0.0.0.0:3000/books/3/pages/1
to this format:
  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
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 rubyguy@ymail.com (Guest)
on 2009-03-22 13:39
(Received via mailing list)
On 22 Mar., 11:16, "Paul A." <rails-mailing-l...@andreas-s.net> 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/ActionControlle...

--
Best regards,
David Knorr
http://twitter.com/rubyguy
51655eeeedad656fad30717c46852437?d=identicon&s=25 Chris Kottom (Guest)
on 2009-03-23 07:58
(Received via mailing list)
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.
This topic is locked and can not be replied to.