Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No route matches [GET] "/users/sign_out" rails 5

When I try to sign out i am getting error of No route matches [GET] "/users/sign_out". this is my link tag for Signout.

 <%= link_to "Sign Out", destroy_user_session_path, method: :get , class:  "nav-link"  %>

Here is what my routes related to my User model and Devise look like:

Rails.application.routes.draw do
  devise_for :users do
    get '/users/sign_out' => 'devise/sessions#destroy'
  end

  root 'books#index'
  resources :books do

    member do
      put "like", to: "books#upvote"
    end

  end
end

And this is my devise.rb

config.sign_out_via = :get
like image 946
Salman Nausher Avatar asked Nov 30 '22 22:11

Salman Nausher


2 Answers

Try the following in routes.rb

devise_for :users
devise_scope :user do
  get '/users/sign_out' => 'devise/sessions#destroy'
end

for more information how to by devise

like image 99
fedesc Avatar answered Dec 07 '22 01:12

fedesc


If you intend to keep the default route path for signout /users/sign_out, then instead of doing this:

devise_for :users do
   get '/users/sign_out' => 'devise/sessions#destroy'
end

The way it should be handled is one of two ways:

  1. Use DELETE method instead of GET

    <%= link_to "Sign Out", destroy_user_session_path, method: :delete, class: "nav-link" %>

-OR-

  1. Edit your devise.rb initializer and change

    config.sign_out_via = :delete to config.sign_out_via = :get

like image 41
Ricky Brown Avatar answered Dec 07 '22 00:12

Ricky Brown