4

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
31piy
  • 21,164
  • 6
  • 40
  • 57
Salman Nausher
  • 141
  • 2
  • 9

5 Answers5

11

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

fedesc
  • 2,270
  • 2
  • 18
  • 34
  • i have to define new method , destroy_user_session_with_get ?? – Salman Nausher Mar 13 '17 at 17:46
  • no... that one you already defined. Just add an alias >>> , as: 'destroy_user_session_with_get' <<< with this rails will route the request with get method. You're simply defining a namespace. The name is just a suggestion ... I'll edit my answer so it's more understandable EDIT: There ... hope it's clearer. If it solved your problem Please be kind and accept answer :) – fedesc Mar 13 '17 at 17:48
  • now i am getting error **undefined local variable or method `destroy_user_session_via_get_path'** in '' @FedeSc – Salman Nausher Mar 13 '17 at 18:02
  • i'll break it down for you -> in your routes.rb write `get '/users/sign_out' => 'devise/sessions#destroy', as: 'destroy_user_session_via_get'` instead of what you wrote. and in view write `` EDIT: gave even a little more polish to answer to make it more understandable – fedesc Mar 13 '17 at 18:04
  • i did what you said still **undefined local variable or method `destroy_user_session_via_get_path** – Salman Nausher Mar 13 '17 at 18:08
  • @SalmanNausher i've edited my answer to a more relevant one. I read the question wrong and didn't noticed the configuration you made in devise initializer. If my edited answer isn't yet the answer please unaccept it so people wont be mislead – fedesc Mar 13 '17 at 18:29
  • Thanx alot it worked. i am more than welcome to accept your answer. – Salman Nausher Mar 13 '17 at 18:42
4

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

Community
  • 1
  • 1
Ricky Brown
  • 599
  • 4
  • 8
3

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.

Vick
  • 31
  • 2
  • 1
    This was my issue. I had updated my `application.js` file for Bootstrap and deleted `//= require jquery //= require jquery_ujs` when adding them back in, it fixed the sign out issue. Thank you. – Christopher Warrington Jan 01 '19 at 01:19
0

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

Question similar to:

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

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

Paulo Belo
  • 1,249
  • 9
  • 11