small medium large xlarge

26 Dec 2008, 22:54
KenA (16 posts)

hi, suppose routes.rb has only: map.resources users, then i rake routes and check the output:

users GET /users {:action=>”index”, :controller=>”users”} POST /users {:action=>”create”, :controller=>”users”} new_user GET /users/new {:action=>”new”, :controller=>”users”} edit_user GET /users/:id/edit {:action=>”edit”, :controller=>”users”} user GET /users/:id {:action=>”show”, :controller=>”users”} PUT /users/:id {:action=>”update”, :controller=>”users”} DELETE /users/:id {:action=>”destroy”, :controller=>”users”}

to make things simple, let’s consider just the last 3 lines:

user GET /users/:id {:action=>”show”, :controller=>”users”} PUT /users/:id {:action=>”update”, :controller=>”users”} DELETE /users/:id {:action=>”destroy”, :controller=>”users”}

now suppose i comment map.resources users and use named routes instead. based on the output above, it makes me believe that i could use 3 named routes with the same name and just differentiate then by its method (get, put e delete):

map.user … :method => :get map.user … :method => :put map.user … :method => :delete

and the links in the views like: (excluding html and erb tags)

link_to ‘delete’, user, :confirm => ‘Confirm?’, :method => :delete or link_to ‘delete’, user_path(user), :confirm => ‘Confirm?’, :method => :delete

well, it doesn’t work, since it’s now named routes, rails will follow the top-first rule and always use GET - SHOW (the delete link will perform a show action) … so i need to use distinct named routes, ex:

map.user_show … map.user_update … map.user_delete …

well, i’m just doing that because i’m studying the rails routes system … i will not do it in a real app … but am i right in my conclusion which is that i can’t emulate the rake routes output like the way i did?

26 Dec 2008, 23:31
SC (15 posts)

Heya Ken,

For the 3 named routes, you didn’t specify the full input for routes.rb, but I assume that you meant…

map.user ‘/users/:id’, :controller => ‘users’, :action => ‘show’, :method => :get map.user ‘/users/:id’, :controller => ‘users’, :action => ‘update’, :method => :put map.user ‘/users/:id’, :controller => ‘users’, :action => ‘destroy’, :method => :delete

Looking at this, I believe you do have to separate the map.(whatever your path name) to different names. Just curious, what kind of errors did Rails give you when the routes didn’t work?

The other thing is you might have to specify out all the variables (eg. :id) as Rails may/may not be able to extract that from user_path(@user) in your different usage instances (eg. for show, delete). Using the shortcut usually means you’ve mapped it to map.resource. Again, not sure, because I’ve not tried this out… because the better way to limit your routes is in Rails 2.2.2 where you can limit which RESTful routes you want to keep.

map.resources :users, :only => [:show, :index, :update]


27 Dec 2008, 09:23
KenA (16 posts)

hi SC … thanks for your thoughts … i just didn’t specified the full rake routes output to save space in the screen, heheh … i didn’t get error msgs, the only thing that happens is that if i click the delete link then the show action takes place … that’s why my conclusion is that i should use different names for path names.(because now it’s named routes, right?)

You must be logged in to comment