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

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


当前回答

我在rails 3.1.0中遇到了同样的问题,我解决了在文件中添加以下行:

app/assets/javascripts/application.js
//= require_tree
//= require jquery
//= require jquery_ujs

其他回答

大多数答案都是片面的。这个问题我已经遇到过很多次了。有两件事需要解决:

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

删除方法需要指定

然后设计使用jquery,所以你需要加载这些

   <%= javascript_include_tag "myDirectiveJSfile" %> 

确保在myDirectiveJSfile.js中同时指定了jquery和jquery-ujs

//= require jquery
//= require jquery_ujs

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

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

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

一般来说,当你得到“没有路由匹配”,但你认为你已经定义了路由,然后再次检查http verb / request方法(是否它的get, put, post, delete等)的路由。

如果您运行rake路由,那么您将看到预期的方法,您可以将其与请求日志进行比较。

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

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

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