I've installed devise on my app and applied the following in my application.html.erb
file:
<div id="user_nav">
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. This cannot be cheese?
<%= link_to 'Sign out', destroy_user_session_path %>
<% else %>
<%= link_to 'Register', new_user_registration_path %> or <%= link_to 'Sign in', new_user_session_path %>
<% end %>
</div>
I ran rake routes
and confirmed that all the routes are valid.
Also, in my routes.rb
file I have devise_for :users
and root :to => "home#index"
.
I get the following routing error when clicking the "Sign out" link:
No route matches "/users/sign_out"
Any ideas what's causing the error?
Check it out with source code in github:
https://github.com/plataformatec/devise/commit/adb127bb3e3b334cba903db2c21710e8c41c2b40#lib/generators/templates/devise.rb (date : June 27, 2011 )
The ability to make the Logout link a DELETE RESTful call requires an html attribute
data-method = "delete"
by using the rails code= link_to('Logout', destroy_user_session_path, :method => :delete)
.However, if you do not have the gem
jquery-ujs
installed or are not calling the resulting javascript in your application.html via= javascript_include_tag "application"
, the response will be sent as a GET request, and the route will fail.You have a few options if you do not want to use
jquery-ujs
or cannot find a way to make it work:config.sign_out_via
to equal:get
withindevise.rb
(not recommended, since DELETE is the appropriate RESTful query)link_to
to= button_to('Logout', destroy_user_session_path, :method => :delete)
. Withbutton_to
Rails will do the heavy lifting on making the proper DELETE call. You can then style the button to look like a link if you wish.Add:
Use these link_to tags
In routes add:
I want to add to this even though it's a bit old.
the "sign_out" link didn't work, despite having :method => :delete.
The comment indicating that
<%= javascript_include_tag :defaults %>
must be included reminded me I had recently added JQuery java script and used simple<script src=""/>
tags to include them.When I moved them from after the :defaults to before, the sign_out started working again.
Hopefully this helps someone.
Lots of solutions are there. but mostly use this,
or config devise.rb with proper sign_out method
In devise.rb