我已经在我的应用程序上安装了设计,并在我的application.html.erb文件中应用了以下内容:

<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>

我运行了rake路由,确认所有路由都是有效的。

同样,在我的路线上。我有belonge_for:users和root:to => "home#index"。

当点击“签出”链接时,我得到以下路由错误:

No route matches "/users/sign_out"

知道是什么导致了这个错误吗?


当前回答

这个问题已经有很多答案了。对我来说,问题有两个方面:

when I expand my routes: devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end 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 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 and of course same link as many mentioned before: <%= link_to "Sign out", destroy_user_session_path, :method => :delete %>

其他回答

这个问题已经有很多答案了。对我来说,问题有两个方面:

when I expand my routes: devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end 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 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 and of course same link as many mentioned before: <%= link_to "Sign out", destroy_user_session_path, :method => :delete %>

尝试添加一个新的路由来设计/sessions#destroy并链接到它。例如:

routes.rb
devise_for :users do
  get 'logout' => 'devise/sessions#destroy'
end

观点:

<%= link_to "Logout", logout_path %>

Add:

  <%= csrf_meta_tag %>  and 
  <%= javascript_include_tag :defaults %>  to layouts

使用这些link_to标记

 link_to 'Sign out', destroy_user_session_path, :method => :delete

  or

 link_to 'Sign out', '/users/sign_out', :method => :delete

在路由中添加:

  devise_for :users do
    get '/users/sign_out' => 'devise/sessions#destroy'
  end

我想补充一点,尽管它有点老了。

“sign_out”链接没有工作,尽管有:method =>:delete。

注释指出<%= javascript_include_tag:defaults %>必须包括在内,这提醒我,我最近添加了JQuery java脚本,并使用简单的<script src=""/>标签来包括它们。

当我把它们从:defaults后面移到前面时,sign_out又开始工作了。

希望这能帮助到一些人。

如果你使用的是HTTPS,如果你的签出链接是不安全的版本,它就会崩溃。在后端,它重定向到安全版本。这个重定向是一个GET,它导致了这个问题。

确保你的链接使用HTTPS。你可以在你的url助手中强制使用协议:“https”(确保你使用url助手而不是路径助手)。

<%= link_to "Sign out", destroy_user_session_url(protocol: "https"), method: :delete %>