Search code examples
ruby-on-railsrubyruby-on-rails-4

Routing trouble for logout link? DELETE vs GET action?


Right now the route for my logout link points to sessions#destroy, but it isn't the HTTP delete verb, so it isn't working. This problem is frustrating me because I should be able to figure it out myself, but I've had no luck so far. Here is the error message I get when trying to log out:

No route matches [DELETE] "/logout"

Here is my routes file:

Rails.application.routes.draw do



  get 'line_items/create'



  root 'static_pages#home'
  get 'testimonials' => 'static_pages#testimonials'
  get 'help' => 'static_pages#help'
  get 'about' => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'signup' => 'users#new'
  get 'login' => 'sessions#new'
  get 'logout' => 'sessions#destroy'
  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  resources :products
  resources :categories
  resources :line_items
  resources :carts
 end

The destroy method in the sessions controller:

def destroy
  forget(current_user)
  session.delete(:user_id)
  @current_user = nil
  redirect_to root_url
end

The logout link:

          <li>
            <%= link_to "Log out", logout_path, method: "delete" %>
          </li>

relevant results of rake routes:

logout_path     GET     /logout(.:format)   sessions#destroy
users_path  GET     /users(.:format)    users#index
POST    /users(.:format)    users#create
new_user_path   GET     /users/new(.:format)    users#new
edit_user_path  GET     /users/:id/edit(.:format)   users#edit
user_path   GET     /users/:id(.:format)    users#show
PATCH   /users/:id(.:format)    users#update
PUT     /users/:id(.:format)    users#update
DELETE  /users/:id(.:format)    users#destroy
sessions_path   POST    /sessions(.:format)     sessions#create
new_session_path    GET     /sessions/new(.:format)     sessions#new
session_path    DELETE  /sessions/:id(.:format)     sessions#destroy 

Thanks for the help.


Solution

  • You have two options:

    1. Change the link to use the defined route

      <%= link_to "Log out", logout_path %>
      

      or

      <%= link_to "Log out", session_path, method: "delete" %>
      

      Please note that in order for the last link to work, you need to change it into a singular resource (resource, not resources).

      resource :sessions, only: [:new, :create, :destroy]
      
    2. Change the route from

      get 'logout' => 'sessions#destroy'
      

      to

      delete 'logout' => 'sessions#destroy'
      

    In any case, the plural resources on sessions does not make sense.