Eric Stermer
Eric Stermer

Reputation: 1101

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

I am currently getting this error using Devise and I have tried multiple things from other questions in order to resolve it with ZERO luck.

I was first advised to make sure I add the method as a delete:

<%= link_to "Logout", destroy_user_session_path, :method => :delete, :class => 'navbar-link'  %>

No luck.

Then I was advised that I needed to include in my layout header this:

<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

or this:

<%= javascript_include_tag :defaults %>

Still... no luck.

Lastly I was advised to change in my /config/initializers/devise.rb file to change config.sign_out_via = :delete to config.sign_out_via = :get

No luck at all.

I am not sure what more to try,

here is my current code in my HTML, let me know if you need to see my routes to better assist you.

    <!DOCTYPE html>
<html>
<header>
  <h2><span>Anume*</span></h2>
  <img id='header' src="http://wallpapercave.com/wp/hkWe2SK.jpg" alt="" />



</header>
  <head>
    <title>Anume</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

<body>
<p class="navbar-text pull-right">
    <% if user_signed_in? %>
    Logged in as
    <strong><%= current_user.email %></strong>.
    <%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %>
    |
    <%= link_to "Logout", destroy_user_session_path, :method => :delete, :class => 'navbar-link'  %>
    <% else %>
    <%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link'  %>
    |
    <%= link_to "Login", new_user_session_path, :class => 'navbar-link'  %>
    <% end %>
</p>
</body>

here are my rails route for the logout function:

destroy_user_session DELETE /users/sign_out(.:format)    devise/session#destroy

Upvotes: 3

Views: 3789

Answers (4)

&#246;yk&#252; parlak
&#246;yk&#252; parlak

Reputation: 71

I got same error too and i tried all of the things that what already wrote but it didn't work. The problem wasn't about ujs, turbo, importmap. Another post guiding me while i was solving the problem.

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

enter link description here

Upvotes: 0

Obonyo
Obonyo

Reputation: 161

If anyone is using Rails 7 change this line

 <%= link_to "Logout", destroy_user_session_path, :method => :delete, :class => 'navbar-link' %>

to

 <%= link_to "Logout", destroy_user_session_path, data: { turbo_method: :delete }, :class => 'navbar-link' %>

Upvotes: 0

Anoob K Bava
Anoob K Bava

Reputation: 605

I had the similar issue, for me it is the issue related with require

jquery_ujs 
require turbolinks

Please check in the

app/assets/javascripts/application.js

below lines are present or not.

//= require jquery
//= require jquery_ujs
//= require turbolinks 

Also Please make sure that, the application.html.erb file,

<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

above line is NOT commented

Upvotes: 0

Adamson Gomez
Adamson Gomez

Reputation: 116

one thing that I like to do on my

/config/initializers/devise.rb is change from config.sign_out_via = :delete to config.sign_out_via = [:delete, :get] so you have both scenarios cover incase the delete method does not hit.

open your console and make sure that jquery and jquery_ujs load properly

let me share my setup with you

in my assets application.js I have

#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks

now on my layout

#app/views/layouts/application.html.haml
!!!
%html{:lang => "en"}
  %head
    %meta{:charset => "utf-8"}/
    %meta{:content => "IE=edge", "http-equiv" => "X-UA-Compatible"}/
    %meta{:content => "width=device-width, initial-scale=1", :name => "viewport"}/
    %title
      Best Site Ever
    = csrf_meta_tags
    = stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload'
    = javascript_include_tag 'application', 'data-turbolinks-track': 'reload'

  %body
    - if current_user
      = link_to destroy_user_session_path, :method => :delete do
        Logout

I hope that this is able to help

Upvotes: 10

Related Questions