Routes and REST Issue

Hi all,

A relative newcomer to routes here - I have a controller on which I
would like to be able to specify only a couple RESTful operations (my
example is for a RESTful user sessions controller, where it only makes
sense to (1) create a session, and (2) destroy a session).

Using ‘resources’ on a controller in the routes file gives all seven
methods so I’m not using that; I only need the two above, so my routes
file looks like this:

match 'sessions' => 'sessions#create', :via => :put
match 'sessions' => 'sessions#destroy', :via => :delete

Essentially I want a user to send a put (this is an idempotent
operation) to the sessions collection URL, with a params hash of
username and password. And calling delete on the same URL will log out
the session.

However, my RSpec test, which looks like this:

describe “PUT ‘new’” do
# (NB this user is created in the ‘before’ method)
it “should be successful” do
put ‘new’, {:name => ‘a_user_logging_in’, :password =>
‘a_password’}
response.should be_success
end

it "should not be successful with a wrong username or password" do
  put 'new', {:name => 'an_incorrect_user', :password =>

‘a_password’}
response.should_not be_success
end
end

Does not pass as the routing fails! Here is the RSpec error:

Failure/Error: put ‘new’, {:name => ‘a_user_logging_in’, :password =>
‘a_password’}
ActionController::RoutingError:
No route matches {:name=>“a_user_logging_in”,
:password=>“a_password”, :controller=>“sessions”, :action=>“new”}

So clearly I’ve made an error here somewhere with my Routes, can someone
help me achieve what I’m after (i.e. just a put and delete on a
collection, which calls create and destroy on the controller?).

Many many thanks for your kind help,

  • Nex

Phil

On Mon, Mar 28, 2011 at 4:52 PM, Peter L. [email protected]
wrote:

match ‘sessions’ => ‘sessions#create’, :via => :put
match ‘sessions’ => ‘sessions#destroy’, :via => :delete

Making a create route does not give you a route for ‘new’ – you’ll need
to
create both.

You can also do:

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

if you like.

On 28 Mar 2011, at 22:52, Peter L. [email protected] wrote:

describe “PUT ‘new’” do
response.should_not be_success
:password=>“a_password”, :controller=>“sessions”, :action=>“new”}

So clearly I’ve made an error here somewhere with my Routes, can someone
help me achieve what I’m after (i.e. just a put and delete on a
collection, which calls create and destroy on the controller?).

You’ve routed put to the create action but in your spec you’re calling
the new action - the 2 need to match up.

Fred

Frederick C. wrote in post #989738:

On 28 Mar 2011, at 22:52, Peter L. [email protected] wrote:

describe “PUT ‘new’” do
response.should_not be_success
:password=>“a_password”, :controller=>“sessions”, :action=>“new”}

So clearly I’ve made an error here somewhere with my Routes, can someone
help me achieve what I’m after (i.e. just a put and delete on a
collection, which calls create and destroy on the controller?).

You’ve routed put to the create action but in your spec you’re calling
the new action - the 2 need to match up.

Fred

This is great, thanks to Phil as well!

  • N