Devise: Why doesn't my logout link work?

jaydel picture jaydel · Aug 4, 2011 · Viewed 39.7k times · Source

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?

Answer

Jay Beale picture Jay Beale · Dec 2, 2011

The correct way to fix this, REST-wise, would be to change your logout links to use the DELETE method. It's a very easy fix, changing this:

link_to "Log out", destroy_user_session_path

to this:

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