Noah Clark
Noah Clark

Reputation: 8131

Action Controller Exception: Using GET instead of DELETE

I have the following for my routes.rb file:

Hchq::Application.routes.draw do
  resources :users
  resources :sessions, only: [:new, :create, :destroy]

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete
end

When I run rake routes I get:

      users GET    /users(.:format)          users#index
            POST   /users(.:format)          users#create
   new_user GET    /users/new(.:format)      users#new
  edit_user GET    /users/:id/edit(.:format) users#edit
       user GET    /users/:id(.:format)      users#show
            PUT    /users/:id(.:format)      users#update
            DELETE /users/:id(.:format)      users#destroy
   sessions POST   /sessions(.:format)       sessions#create
new_session GET    /sessions/new(.:format)   sessions#new
    session DELETE /sessions/:id(.:format)   sessions#destroy
     signup        /signup(.:format)         users#new
     signin        /signin(.:format)         sessions#new
    signout DELETE /signout(.:format)        sessions#destroy

and yet, when I go to http://localhost:3000/signout I get the following:

routing error: Action Controller Exception

In the log I get: ActionController::RoutingError (No route matches [GET] "/signout"):

Which, shouldn't I be having a [DELETE] instead of a [GET] on the /signout route? If so how do I change it?

Upvotes: 0

Views: 360

Answers (1)

Agis
Agis

Reputation: 33656

By navigating straight from your browser to that address, you're actually performing a GET request. Browsers by default are submitting GET requests when a user enters a URL and ask it to render the page.

You should insert a link in your view using #link_to and passing the DELETE method as an option, like this:

link_to "Signout", signout_path, :method => :delete

Upvotes: 5

Related Questions