Devise: Why doesn't my logout link work?

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?


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 

I had the same problem with rails 3.2 when I deleted from application.js this line:

//= require jquery_ujs

So, I think you have to insert this line in your application.js if you haven't it there.

PS. This behavior means that rails adapter for jquery doesn't function. So you should make sure if it is loaded in your html in browser. You should test it in development mode because you will have compressed js in production and it will be very difficult to find something there.