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?
I know this is an old question based on Rails 3 but I just ran into and solved it on Rails 4.0.4. So thought I'd pitch in how I fixed it for anyone encountering this problem with this version. Your mileage may vary but here's what worked for me.
First make sure you have the gems installed and run bundle install.
In application.js check that everything is required like below.
Beware if this gotcha: it's
//= require jquery.turbolinks
and not//= require jquery-turbolinks
Next, add the appropriate links in the header of application.html.erb.
There seems to be many variations on how to implement the delete method which I assume depends on the version of Rails you are using. This is the
delete
syntax I used.Hope that helps dig someone out of this very frustrating hole!
I think the route for signing out is a
DELETE
method. This means that your sign out link needs to look like this:Yours doesn't include the
:method => :delete
part. Also, please note that for this to work you must also include<%= javascript_include_tag :defaults %>
in your layout file (application.html.erb
).See if your routes.rb has a "resource :users" before a "devise_for :users" then try swapping them:
Works
Fails
Other option is to configure the logout to be a GET instead a DELETE, you can do that adding the following line on
/config/initializers/devise.rb
But as Steve Klabnik wrote on his blog (http://blog.steveklabnik.com/2011/12/11/devise-actioncontroller-routingerror-no-route-matches-get-slash-users-slash-sign-out.html) try to use DELETE because of the semantic of this method.
This means you haven't generated the jquery files after you have installed the jquery-rails gem. So first you need to generate it.
rails generate devise:install
First Option:
This means either you have to change the following line on
/config/initializers/devise.rb
config.sign_out_via = :delete to config.sign_out_via = :get
Second Option:
You only change this line
<%= link_to "Sign out", destroy_user_session_path %>
to<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
on the view file.Usually
:method => :delete
is not written by default.use
:get
and:delete
method for your path: