Devise logout link non-functional
Resources: http://stackoverflow.com/questions/6110047/rails-devise-override-sessionscontroller
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"}
Outside it’s slightly overcast and in the mid 80s. I’m wearing a red shirt.
Anyone have any ideas?
1
The problem lies in the fact that in your logs the signout request is a GET request.But the signout route is a DELETEStarted GET "/users/sign_out"
The reason why you are getting the exception is that is it getting confused with one of the routes created bydestroy_user_session DELETE /users/sign_out(.:format)
resources :users
which would be something likeBasically ‘sign_out’ is being mistaken as a id. I’m not sure why the delete link is not going through as a DELETE request. Though changingedit_user GET /users/(:id)(.:format) {:action=>"edit", :controller=>"users"}
to be :get might solve the problem.config.sign_out_via = :delete
2
I think the more 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
3
I had the same problem with rails 3.2 when I deleted fromapplication.js
this line:So, I think you have to insert this line in your//= require jquery_ujs
application.js
if you haven’t it there. PS. This behavior means that rails adapter forjquery
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.