简体   繁体   English

没有路线匹配“/users/sign_out” devise rails 3

[英]No route matches "/users/sign_out" devise rails 3

I've installed devise on my app and applied the following in my application.html.erb file:我已经在我的应用程序上安装了 devise 并在我的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 routes and confirmed that all the routes are valid.我运行了rake routes并确认所有路线都是有效的。

Also, in my routes.rb file I have devise_for:users and 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?任何想法是什么导致了错误?

I think the route for signing out is a DELETE method.我认为退出的路线是DELETE方法。 This means that your sign out link needs to look like this:这意味着您的退出链接需要如下所示:

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

Yours doesn't include the :method =>:delete part.你的不包括:method =>:delete部分。 Also, please note that for this to work you must also include <%= javascript_include_tag:defaults %> in your layout file ( application.html.erb ).另外,请注意,要使其正常工作,您还必须在布局文件( application.html.erb )中包含<%= javascript_include_tag:defaults %>

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.它开始为我工作。

You probably didn't include jquery_ujs javascript file.您可能没有包含 jquery_ujs javascript 文件。 Make sure you are using the latest version of jquery-ujs: https://github.com/rails/jquery-ujs and the last files available:确保您使用的是最新版本的 jquery-ujs: https://github.com/rails/jquery-ujs和最后可用的文件:

rails generate jquery:install

You should not have any more rails.js file.你不应该有 rails.js 文件了。 If you do, you're probably out-of-date.如果你这样做,你可能已经过时了。 Make sure also this file is loaded with defaults, in config/application.rb确保这个文件也加载了默认值,在 config/application.rb

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

(Again, you should not have rails.js file here). (同样,这里应该有 rails.js 文件)。 Finally, add the link as documented on Devise wiki (haml-style):最后,添加Devise wiki (haml 样式)中记录的链接:

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

And everything will be fine.一切都会好起来的。

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) .使注销链接成为 DELETE RESTful 调用的能力需要 html 属性data-method = "delete"使用 rails code = link_to('Logout', destroy_user_session_path, :method =>:delete)

However, if you do not have the gem jquery-ujs installed 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.但是,如果您没有安装 gem jquery-ujs或者没有在您的 application.html via = javascript_include_tag "application"中调用生成的 javascript ,则响应将作为 GET 请求发送,并且路由将失败。

You have a few options if you do not want to use jquery-ujs or cannot find a way to make it work:如果您不想使用jquery-ujs或找不到使其工作的方法,您有几个选择:

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

Try adding a new route to devise/sessions#destroy and linking to that.尝试添加一条新路由来设计/会话#destroy 并链接到该路由。 Eg:例如:

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

view:看法:

<%= link_to "Logout", logout_path %>

Use it in your routes.rb file:在您的 routes.rb 文件中使用它:

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

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

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

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.我想我会删除我的 jQuery。 Bad choice.糟糕的选择。 Devise is using jQuery to "fake" DELETE request and send it as GET. Devise 正在使用 jQuery 来“伪造”删除请求并将其作为 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 %>

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

Other option is to configure the logout to be a GET instead a DELETE, you can do that adding the following line on /config/initializers/devise.rb其他选项是将注销配置为 GET 而不是 DELETE,您可以在/config/initializers/devise.rb上添加以下行

config.sign_out_via = :get

But as Steve Klabnik wrote on his blog (http://blog.steveklabnik.com/2011/12/11/devise-actioncontroller-routingerror-no-route-matches-get-slash-users-slash-sign-out.html) try to use DELETE because of the semantic of this method.但正如 Steve Klabnik 在他的博客 (http://blog.steveklabnik.com/2011/12/11/devise-actioncontroller-routingerror-no-route-matches-get-slash-users-slash-sign-out.html ) 由于此方法的语义,请尝试使用 DELETE。

If you are using Rails 3.1 make sure your application.html.erb sign out looks like:如果您使用的是 Rails 3.1,请确保您的 application.html.erb 退出看起来像:

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

And that your javascript include line looks like the following并且您的 javascript 包含行如下所示

<%= javascript_include_tag 'application' %>

My guess is that some gems overwrite the new structure of the default.js location.我的猜测是某些 gem 会覆盖 default.js 位置的新结构。

Don't forget to include the following line in your application.js (Rails 3)不要忘记在您的 application.js (Rails 3) 中包含以下行

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

Include jquery_ujs into my rails application and it works now. jquery_ujs包含到我的 rails 应用程序中,它现在可以工作了。

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然后 devise 使用 jquery,所以你需要加载那些

   <%= javascript_include_tag "myDirectiveJSfile" %> 

and ensure that BOTH jquery and jquery-ujs are specified in your myDirectiveJSfile.js并确保在 myDirectiveJSfile.js 中同时指定 jquery 和 jquery-ujs

//= require jquery
//= require jquery_ujs

Check it out with source code in github:查看 github 中的源代码:

https://github.com/plataformatec/devise/commit/adb127bb3e3b334cba903db2c21710e8c41c2b40#lib/generators/templates/devise.rb (date: June 27, 2011 ) https://github.com/plataformatec/devise/commit/adb127bb3e3b334cba903db2c21710e8c41c2b40#lib/generators/templates/devise.rb (日期:2011年6月27日)

  • # The default HTTP method used to sign out a resource. # 用于注销资源的默认 HTTP 方法。 Default is:get.默认为:获取。 188 188
  • # config.sign_out_via =:get 187 # config.sign_out_via =:get 187
  • # The default HTTP method used to sign out a resource. # 用于注销资源的默认 HTTP 方法。 Default is:delete.默认为:删除。 188 188
  • config.sign_out_via =:delete config.sign_out_via =:删除

Well, guys for me it was only remove the:method =>:delete好吧,对我来说,伙计们只是删除:方法=>:删除

<%= link_to('Sign out', destroy_user_session_path) %>

I want to add to this even though it's a bit old.我想补充一点,即使它有点旧了。

the "sign_out" link didn't work, despite having:method =>:delete. “sign_out”链接不起作用,尽管有:method =>:delete。

The comment indicating that <%= javascript_include_tag:defaults %> must be included reminded me I had recently added JQuery java script and used simple <script src=""/> tags to include them.指示必须包含<%= javascript_include_tag:defaults %>的注释提醒我我最近添加了 JQuery java 脚本并使用简单的<script src=""/>标签来包含它们。

When I moved them from after the:defaults to before, the sign_out started working again.当我将它们从:defaults 之后移到之前时,sign_out 再次开始工作。

Hopefully this helps someone.希望这可以帮助某人。

This means you haven't generated the jquery files after you have installed the jquery-rails gem.这意味着您在安装 jquery-rails gem 后还没有生成 jquery 文件。 So first you need to generate it.所以首先你需要生成它。

rails generate devise:install

First Option:第一个选项:

This means either you have to change the following line on /config/initializers/devise.rb这意味着您必须更改/config/initializers/devise.rb上的以下行

config.sign_out_via =:delete to config.sign_out_via =:get config.sign_out_via =:删除到 config.sign_out_via =:get

Second Option:第二种选择:

You only change this line <%= link_to "Sign out", destroy_user_session_path %> to <%= link_to "Sign out", destroy_user_session_path, :method =>:delete %> on the view file.您只需在视图文件中将这一行<%= link_to "Sign out", destroy_user_session_path %>更改为<%= link_to "Sign out", destroy_user_session_path, :method =>:delete %>

Usually :method =>:delete is not written by default.通常:method =>:delete默认不写。

If you're using HTTPS with devise , it'll break if your sign-out link is to the non-secure version.如果您将HTTPS 与 devise 一起使用,则如果您的注销链接指向非安全版本,它将中断。 On the back end, it redirects to the secure version.在后端,它重定向到安全版本。 That redirect is a GET, which causes the issue.该重定向是一个 GET,这会导致问题。

Make sure your link uses HTTPS.确保您的链接使用 HTTPS。 You can force it with protocol: "https" in your url helper (make sure you use the url helper and not the path helper).您可以在 url 助手中使用protocol: "https" (确保使用 url 助手而不是路径助手)。

<%= link_to "Sign out", destroy_user_session_url(protocol: "https"), method: :delete %>
  devise_for :users
  devise_scope :user do
    get '/users/sign_out' => 'devise/sessions#destroy'
  end

Lots of solutions are there.有很多解决方案。 but mostly use this,但大多使用这个,

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

or config devise.rb with proper sign_out method或使用正确的签出方法配置 devise.rb

In devise.rb在 devise.rb

config.sign_out_via = :delete ( or  :get which u like to use.) 

use :get and :delete method for your path:为您的路径使用:get:delete方法:

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

The problem begin with rails 3.1... in /app/assets/javascript/ just look for application.js.问题从/app/assets/javascript/中的 rails 3.1... 开始,只需查找 application.js。

If the file doesn't exist create a file with that name I don't know why my file disappear or never was created on "rails new app"... .如果该文件不存在,请创建一个具有该名称的文件,我不知道为什么我的文件会消失或从未在"rails new app"...

That file is the instance for jquery... .该文件是jquery...的实例。

In your routes.rb:在您的 routes.rb 中:

 devise_for :users do
    get '/sign_out' => 'devise/sessions#destroy'
    get '/log_in' => 'devise/sessions#new'
    get '/log_out' => 'devise/sessions#destroy'
    get '/sign_up' => 'devise/registrations#new'
    get '/edit_profile' => 'devise/registrations#edit'
 end

and in your application.html.erb:并在您的 application.html.erb 中:

<%if user_signed_in?%>
          <li><%= link_to "Sign_out", sign_out_path %></li>
<% end %>

This is what I did (with Rails 3.0 and Devise 1.4.2):这就是我所做的(使用 Rails 3.0 和 Devise 1.4.2):

  1. Make sure your page loads rails.js确保您的页面加载 rails.js
  2. Use this param: 'data-method' => 'delete'使用这个参数:'data-method' => 'delete'
  3. Good idea to add this param: :rel => 'nofollow'添加此参数的好主意: :rel => 'nofollow'

See if your routes.rb has a "resource:users" before a "devise_for:users" then try swapping them:查看您的 routes.rb 在“devise_for:users”之前是否有“resource:users”,然后尝试交换它们:

  1. Works作品

    • devise_for:users设计:用户
    • resources:users资源:用户
  2. Fails失败

    • resources:users资源:用户
    • devise_for:users设计:用户

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页面中的':method =>:delete ' 是 ' data-method="delete" ' 所以你的页面必须有 jquery_ujs.js,它将使用方法 delete 而不是方法 get 提交链接

I know this is an old question based on Rails 3 but I just ran into and solved it on Rails 4.0.4.我知道这是一个基于 Rails 3 的老问题,但我只是在 Rails 4.0.4 上遇到并解决了它。 So thought I'd pitch in how I fixed it for anyone encountering this problem with this version.所以我想我会为遇到此版本问题的任何人介绍我如何修复它。 Your mileage may vary but here's what worked for me.您的里程可能会有所不同,但这对我有用。

First make sure you have the gems installed and run bundle install.首先确保您已安装 gems 并运行 bundle install。

gem 'jquery-rails'

gem 'turbolinks'

gem 'jquery-turbolinks'

In application.js check that everything is required like below.在 application.js 中检查是否需要如下所示的所有内容。

Beware if this gotcha : it's //= require jquery.turbolinks and not //= require jquery-turbolinks当心这个问题:它//= require jquery.turbolinks而不是//= require jquery-turbolinks

//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require turbolinks
//= require_tree .

Next, add the appropriate links in the header of application.html.erb.接下来,在 application.html.erb 的 header 中添加相应的链接。

<%= javascript_include_tag  "application", "data-turbolinks-track" => true %>
<%= javascript_include_tag :defaults %>

There seems to be many variations on how to implement the delete method which I assume depends on the version of Rails you are using.关于如何实现 delete 方法似乎有很多变化,我认为这取决于您使用的 Rails 版本。 This is the delete syntax I used.这是我使用的delete语法。

<p><%= link_to "Sign Out", destroy_user_session_path, :method => 'delete' %></p>

Hope that helps dig someone out of this very frustrating hole!希望这有助于将某人从这个非常令人沮丧的洞中挖出来!

In general when you get "No route matches" but you think you have that route defined then double check the http verb / request method (whether its get, put, post, delete etc.) for that route .通常,当您收到“无路由匹配”但您认为已定义该路由时,请仔细检查该路由的 http 动词/请求方法(无论是获取、放置、发布、删除等)

If you run rake routes then you will see the expected method and you can compare this with the request log.如果您运行 rake 路由,那么您将看到预期的方法,您可以将其与请求日志进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails设计错误“没有路由匹配[GET]”/ users / sign_out“” - Rails Devise Error “No route matches [GET] ”/users/sign_out“” Rails 3 with Devise:没有路由匹配/ d / users / sign_out - Rails 3 with Devise: No route matches /d/users/sign_out Rails 3 Devise-获取“没有路线匹配” / users / sign_out“” - Rails 3 Devise - Getting “ No route matches ”/users/sign_out“ ” 设备-没有路线与[GET]“ / users / sign_out”匹配-Rails 4 - DEVISE - No route matches [GET] “/users/sign_out” - Rails 4 设计/Rails:没有路由匹配 [GET] “/users/sign_out” - Devise/Rails: No route matches [GET] “/users/sign_out” 无法使用Devise gem在Rails应用中注销,没有路线与/ users / sign_out匹配 - Unable to sign out in a Rails app, using Devise gem, no route matches /users/sign_out 设计没有路线匹配[GET]“/ users / sign_out” - devise No route matches [GET] “/users/sign_out” 设计路由错误:没有路由与[GET]“ / users / sign_out”匹配 - Devise routing error: No route matches [GET] “/users/sign_out” 在Rails 3.1中设计路线问题,没有路线匹配[GET]“ / users / sign_out” - Devise routes issue in Rails 3.1 with No route matches [GET] “/users/sign_out” Rails Devise,没有路由匹配&#39;/users/sign_out&#39; -&gt; SignOut/Logout Routes 问题 - Rails Devise, No route matches '/users/sign_out' -> SignOut/Logout Routes problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM