Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise Sign Out Error - The action 'show' could not be found for UsersController

Devise generated the following code to sign a user out:

<%= link_to "Sign out", destroy_user_session_path %>

And the route appears when executing rake routes

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

However I'm getting an error saying:

The action 'show' could not be found for UsersController

Any ideas?

like image 609
Elliot Avatar asked Nov 04 '11 00:11

Elliot


1 Answers

the path is correct but if you look closely you see that it's not a GET request but a DELETE request so pass the method:

<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>

Edit:

This should add a data-method="delete" attribute to your link. Verify that (have a look at the generated HTML). If the attribute is present and nothing happens if you click on that link, then ensure that you've included the default javascript files in your layout. There should be a line like:

<%= javascript_include_tag :defaults %>

in your layout.

>>Important<<: You can't type the logout url into your address bar and hit enter, it won't work because it's a GET and not a DELETE request. The magic behind this is that a javascript helper will hook on to the "onclick" event of the link and then submit a hidden form (via POST) to the href destination of the url containing a hidden field called _method with the value "delete".

Why all this? It's a security thing, otherwise someone could redirect you to the logout page and simply log you out and all your unsaved session stuff will be gone...

If you reeeeaaaallllyyy need a logout via GET then add this to your

config/initializers/devise.rb:

config.sign_out_via = :get

like image 175
sled Avatar answered Oct 15 '22 07:10

sled