Ruby-on-rails 没有路由匹配 "/users/sign_out" 设计 rails 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6557311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:31:10  来源:igfitidea点击:

No route matches "/users/sign_out" devise rails 3

ruby-on-railsruby-on-rails-3deviseroutes

提问by vich

I've installed devise on my app and applied the following in my application.html.erbfile:

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

I ran rake routesand confirmed that all the routes are valid.

我跑了rake routes并确认所有路线都有效。

Also, in my routes.rbfile I have devise_for :usersand root :to => "home#index".

另外,在我的routes.rb文件中,我有devise_for :usersroot :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?

任何想法是什么导致错误?

回答by Jessie Dedecker

I think the route for signing out is a DELETEmethod. This means that your sign out link needs to look like this:

我认为退出的途径是一种DELETE方法。这意味着您的注销链接需要如下所示:

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

Yours doesn't include the :method => :deletepart. Also, please note that for this to work you must also include <%= javascript_include_tag :defaults %>in your layout file (application.html.erb).

你的不包括这:method => :delete部分。另外,请注意,要使其正常工作,您还必须将其包含<%= javascript_include_tag :defaults %>在布局文件 ( application.html.erb) 中。

回答by kitdesai

I changed this line in devise.rb:

我在 devise.rb 中更改了这一行:

config.sign_out_via = :delete

to

config.sign_out_via = :get

and it started working for me.

它开始对我来说有效。

回答by Gravis

You probably didn't include jquery_ujs javascript file. Make sure you are using the latest version of jquery-ujs : https://github.com/rails/jquery-ujsand the last files available :

您可能没有包含 jquery_ujs javascript 文件。确保您使用的是最新版本的 jquery-ujs:https: //github.com/rails/jquery-ujs和最后可用的文件:

rails generate jquery:install

You should nothave any more rails.js file. If you do, you're probably out-of-date. Make sure also this file is loaded with defaults, in config/application.rb

您不应有任何 rails.js 文件。如果你这样做,你可能已经过时了。确保这个文件也加载了默认值,在 config/application.rb

config.action_view.javascript_expansions[:defaults] = %w(jquery.min jquery_ujs)

(Again, you should nothave rails.js file here). Finally, add the link as documented on Devise wiki(haml-style):

(同样,你应该不会有rails.js文件在这里)。最后,添加Devise wiki(haml-style)上记录的链接:

= link_to('Logout', destroy_user_session_path, :method => 'delete')

And everything will be fine.

一切都会好起来的。

回答by Will Nathan

The ability to make the Logout link a DELETE RESTful call requires an html attribute data-method = "delete"by using the rails code = link_to('Logout', destroy_user_session_path, :method => :delete).

使 Logout 链接成为 DELETE RESTful 调用的能力需要data-method = "delete"使用 rails 代码的 html 属性= link_to('Logout', destroy_user_session_path, :method => :delete)

However, if you do not have the gem jquery-ujsinstalled or are not calling the resulting javascript in your application.html via = javascript_include_tag "application", the response will be sent as a GET request, and the route will fail.

但是,如果您没有jquery-ujs安装gem或者没有通过 调用 application.html 中生成的 javascript = javascript_include_tag "application",响应将作为 GET 请求发送,并且路由将失败。

You have a few options if you do not want to use jquery-ujsor cannot find a way to make it work:

如果您不想使用jquery-ujs或找不到使其工作的方法,您有几个选择:

  1. Change config.sign_out_viato equal :getwithin devise.rb(not recommended, since DELETE is the appropriate RESTful query)
  2. OR Change the link_toto = button_to('Logout', destroy_user_session_path, :method => :delete). With button_toRails will do the heavy lifting on making the proper DELETE call. You can then style the button to look like a link if you wish.
  1. 更改config.sign_out_via等于:getdevise.rb(不推荐,因为DELETE是适当的RESTful查询)
  2. 或 将 更改link_to= button_to('Logout', destroy_user_session_path, :method => :delete)。使用button_toRails 将完成正确的 DELETE 调用的繁重工作。然后,您可以根据需要将按钮的样式设置为看起来像一个链接。

回答by Kevin Tsoi

Try adding a new route to devise/sessions#destroy and linking to that. Eg:

尝试添加一条新路线来设计/会话#destroy 并链接到该路线。例如:

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

view:

看法:

<%= link_to "Logout", logout_path %>

回答by Uma Maheswararao

Use it in your routes.rb file:

在你的 routes.rb 文件中使用它:

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

回答by gringo

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

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

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

回答by Galen

With one exception, Jessie's answer worked for me:

除了一个例外,杰西的回答对我有用:

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

change:

改变:

:delete

... to:

... 到:

'delete'

So the code that worked for me is:

所以对我有用的代码是:

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

回答by Lukasz Muzyka

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 %>
    
  1. 当我扩展我的路线时:

    devise_for :users do 
       get '/users/sign_out' => 'devise/sessions#destroy'
    end
    
  2. 我收到警告说这是折旧的,所以我把它替换为:

    devise_scope :users do
       get '/users/sign_out' => 'devise/sessions#destroy'
    end
    
  3. 我以为我会删除我的 jQuery。糟糕的选择。Devise 正在使用 jQuery 来“伪造”DELETE 请求并将其作为 GET 发送。因此,您需要:

    //= require jquery
    //= require jquery_ujs
    
  4. 当然,与之前提到的许多链接相同:

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

回答by Amal Kumar S

Add:

添加:

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

Use these link_to tags

使用这些 link_to 标签

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

  or

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

In routes add:

在路由中添加:

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