Devise: Why doesn't my logout link work?

Viewed 43747

the problem: In a nutshell, when I try to install a logout link to my app it fails to work. Here's as much context as I can think to put here (if you want anything else, please poke me)...

I've got this in a haml view:

= link_to("Logout", destroy_user_session_path, :method => :delete)

It generates this in the view:

<a href="/users/sign_out" data-method="delete" rel="nofollow">Logout</a>

I verified that in my config/initializers/devise.rb I have this setting uncommented and correct:

config.sign_out_via = :delete

I validated the following route:

destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}

I also have this bit of trickery in my routes.rb, and I suspect this is related to my issue:

devise_for :users, :controllers => {:sessions => "devise/sessions", :registrations => "users"}
resources :users

This last bit is because I want to manage (edit, create and delete) users in my own controller.

The error message I'm getting is as follows:

ActiveRecord::RecordNotFound in UsersController#show

Couldn't find User with ID=sign_out
Rails.root: /home/jaydel/projects/mbsquared-projects/Wilson-Goldrick

app/controllers/users_controller.rb:16:in `show'

In my server logs I see this for the request:

Started GET "/users/sign_out" for 127.0.0.1 at 2011-08-04 13:08:51 -0500
  Processing by UsersController#show as HTML
  Parameters: {"id"=>"sign_out"}

Anyone have any ideas?

19 Answers

I've tried all the solutions mentioned here but the true solution to this issue is the following :

<%= link_to "Sign Out", destroy_user_session_path, 'data-turbo-method': :delete%>

It works if it doesn't just refresh or restart the server.

As mentioned by Tombart above, the answer for me was putting back the rails-ujs call in my application.js file.

//= require rails-ujs
//= require turbolinks

I had initially removed them to stop errors from having my scripts in the footer be re-evaluated. My workaround was to create a footer.js compile file with the resources I wanted in the footer.

//= require jquery-3.2.1.min
//= require jquery-ui-1.12.1.min
//= require materialize
//= require init

Then in my /initializers/assets.rb file adding a precompile

Rails.application.config.assets.precompile += %w( footer.js )

Then you can call the following in your layout/application.html.erb file.

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'footer' %>

I keep the application line in my header, and added my new "footer" call before the end of my body tag.

Stop the server
yarn add  "@rails/ujs"
yarn add turbolinks
yarn add @rails/activestorage
My application.js
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"

Rails.start()
Turbolinks.start()
ActiveStorage.start()
Start the server
Related