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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With