No route matches “/users/sign_out” devise rails 3

2019-01-01 17:05发布

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?

29条回答
人气声优
2楼-- · 2019-01-01 17:10

If you are using Rails 3.1 make sure your application.html.erb sign out looks like:

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

And that your javascript include line looks like the following

<%= javascript_include_tag 'application' %>

My guess is that some gems overwrite the new structure of the default.js location.

查看更多
荒废的爱情
3楼-- · 2019-01-01 17:11

I had the same problem with rails 3.1.0, and I solved adding in file the followings lines:

app/assets/javascripts/application.js
//= require_tree
//= require jquery
//= require jquery_ujs
查看更多
闭嘴吧你
4楼-- · 2019-01-01 17:12

the ':method => :delete' in page is 'data-method="delete"' so your page must have jquery_ujs.js, it will submit link with method delete not method get

查看更多
牵手、夕阳
5楼-- · 2019-01-01 17:14

Use it in your routes.rb file:

devise_for :users do
    get '/users/sign_out' => 'devise/sessions#destroy'
end
查看更多
永恒的永恒
6楼-- · 2019-01-01 17:14

Many answers to the question already. For me the problem was two fold:

  1. when I expand my routes:

    devise_for :users do 
       get '/users/sign_out' => 'devise/sessions#destroy'
    end
    
  2. I was getting warning that this is depreciated so I have replaced it with:

    devise_scope :users do
       get '/users/sign_out' => 'devise/sessions#destroy'
    end
    
  3. I thought I will remove my jQuery. Bad choice. Devise is using jQuery to "fake" DELETE request and send it as GET. Therefore you need to:

    //= require jquery
    //= require jquery_ujs
    
  4. and of course same link as many mentioned before:

    <%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
    
查看更多
皆成旧梦
7楼-- · 2019-01-01 17:16

Most answers are partial. I have hit this issue many times. Two things need to be addressed:

<%= link_to(t('logout'), destroy_user_session_path, :method => :delete) %>

the delete method needs to be specified

Then devise uses jquery, so you need to load those

   <%= javascript_include_tag "myDirectiveJSfile" %> 

and ensure that BOTH jquery and jquery-ujs are specified in your myDirectiveJSfile.js

//= require jquery
//= require jquery_ujs
查看更多
登录 后发表回答