我已经在我的应用程序上安装了设计,并在我的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"

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


当前回答

我认为退出的路由是一个DELETE方法。这意味着你的签出链接需要看起来像这样:

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

你的不包括:method =>:delete部分。另外,请注意,要实现这个功能,你还必须在布局文件(application.html.erb)中包含<%= javascript_include_tag:defaults %>。

其他回答

我认为退出的路由是一个DELETE方法。这意味着你的签出链接需要看起来像这样:

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

你的不包括:method =>:delete部分。另外,请注意,要实现这个功能,你还必须在布局文件(application.html.erb)中包含<%= javascript_include_tag:defaults %>。

对路径使用:get和:delete方法:

devise_scope :user do
  match '/users/sign_out' => 'devise/sessions#destroy', :as => :destroy_user_session, via: [:get, :delete]
end

For Rails 7

我们在importmap中手动导入jquery和jquery-ujs。rb:

pin "jquery", to: "https://ga.jspm.io/npm:jquery@3.6.0/dist/jquery.js"
pin "jquery-ujs", to: "https://cdnjs.cloudflare.com/ajax/libs/jquery-ujs/1.2.3/rails.min.js"

或者对于jquery使用:

./bin/importmap pin jquery

然后手动输入jquery-ujs的第二行

我们用下面的代码创建文件:app/javascript/src/jquery.js:

import jquery from 'jquery'
window.jQuery = jquery
window.$ = jquery

然后我们将它们导入app/javascript/application.js:

import "src/jquery"
import "jquery-ujs"

我们不需要改变配置就完成了。Sign_out_via =:delete of device .rb

**注意:对于jquery可能需要一个yarn,如果由于某些原因上面的不工作

不要忘记在你的application.js (Rails 3)中包含下面这行代码

//= require_self
//= require jquery
//= require jquery_ujs

将jquery_ujs包含到我的rails应用程序中,它现在可以工作了。

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

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

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