Limiting resource routes and adding additional non-RESTful routes in Rails 3

I was not able to find anything here or elsewhere that encompassed both restricting resource routes and adding additional non-RESTful routes in Rails 3. This is probably very simple, but every example or explanation I came across concerns only one case, not both at the same time.

Here is an example of what I did in Rails 2:

map.resources :sessions, :only => [:new, :create, :destroy], :member => {:recovery => :get}

Pretty simple, we only need 3 of 7 RESTful routes, because others do not make any sense for this resource, but we also want to add another route that is used to restore the account.

Now from what I'm going to do, one of these things is very simple:

resources :sessions, :only => [:new, :create, :destroy]

Like Rails 2. And:

 resources :sessions do member do get :recovery end end 

So how do I combine these two? Can I use the old Rails 2 method? Is there a preferred way to do this in Rails 3?

+8
ruby ruby-on-rails routes
source share
2 answers

You can pass arguments and a block to resources :

 resources :sessions, :only => [:new, :create, :destroy] do get :recovery, :on => :member end 

And check it with rake routes .

+15
source share

It should work something like this:

 resources :sessions, :only => [:new, :create, :destroy] do member do get :recovery end end 

There is an even shorter path suggested by coreyward.

Check out the rail guides, Rails Routing from Outside In . "I can also recommend" The Rails 3 Way "from Obie Fernandez, who got 2 pretty good chapters on routing and RESt.

Greetings

+5
source share

All Articles