Forum: Ruby on Rails RESTful Routing: Getting names of resources out of paths

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Chris H. (Guest)
on 2009-04-18 22:11
Hello -

I'm trying to remain RESTful in a new application that I'm building, but
I'm not sure how to get the routes that I want. I'm trying to avoid
having resource names like "posts" and "comments" cluttering things up.

For example, for a resource that I designate:

GET -> index
GET -> show, id=1
GET -> edit, id=1
GET -> new

Etc. In addition, I'd like to nest another resource beneath that
(there's a has_many relationship), so that I can get routes like:

Etc. I'm planning on using friendly_id to change the IDs and make the
URLs more memorable, but before I introduce that I just want to get
those resource names out of there. I want to remain as restful as
possible, but I'm not sure how to get this using map.resources.


Dmitry S. (Guest)
on 2009-04-21 18:00
(Received via mailing list)
Hi Chris,

You can't do it using Rails' map.resources macro. You need to use the
router directly, like this:

map.posts '', :controller => 'posts', :action => 'index', :conditions
=> {:method => :get}
map.posts '', :controller => 'posts', :action => 'create', :conditions
=> {:method => :post}
map.new_post 'new', :controller => 'posts', :action =>
'new', :conditions => {:method => :get} ':id', :controller => 'posts', :action => 'show', :conditions
=> {:method => :get} ':id', :controller => 'posts', :action =>
'update', :conditions => {:method => :put} ':id', :controller => 'posts', :action =>
'destroy', :conditions => {:method => :delete}
map.edit_post ':id/edit', :controller => 'posts', :action =>
'edit', :conditions => {:method => :get}
map.resources :comments, :path_prefix => ':post_id'

it will work, but generally it's not a very good idea to have such
URLs, cause it won't work very well if you will have lots of resources
at some point. Maybe it will be better to use standard routes + 2
aliases, for index (/ — shows all posts) and for show (/123 - shows a
specific post).

This topic is locked and can not be replied to.