4
votes

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
5

5 Answers

12
votes

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

4
votes

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

3
votes

Make sure of this:

in your Gemfile include

gem 'jquery-rails' 

in application.js

//= require jquery 
//= require jquery_ujs

In my opinion is not so good change the routers or the method to sign out, this action should be done always with DELETE. That worked for me.

0
votes

Since you are destroying the session, it is not a :get method but a :delete. Modify the link to:

= link_to "Log out", destroy_user_session_path, method: :delete

0
votes

Question similar to:

No route matches [GET] “/users/sign_out”

Please check my answer there (the one here got deleted)