没有路线匹配[GET]“/ users / sign_out”

时间:2011-07-04 06:07:18

标签: authentication devise ruby-on-rails-3.1

以下是我的实际错误:No route matches [GET] "/members/sign_out" 由于大多数人会使用“用户”,我认为在标题中使用它会更有帮助。无论如何,我必须无法注销。我可以成功编辑我的会员资料。

我正在使用设计1.4.2和Rails 3.1.0.rc4。此外,我已经生成了两个独立的设计模型 - 一个称为“成员”,另一个称为“管理员”。我能够通过手动导航到正确的URL路径(即localhost:3000 / admins / sign_in /)来注册并登录它们(同时)。我按照RailsCast on Devise在我的application.html.haml布局文件中创建了一些链接。我知道它只涉及“成员”的登录/注销链接。

如果我点击退出链接,我会收到上述错误。如果我手动导航到任何一个注销URL(即localhost:3000 / admins / sign_out /),就会发生这种情况。

有人能告诉我为什么会这样吗?以下是各种相关文件。当然,我是新手......

rake路线输出:

    j(film_repo)$ rake routes
        new_member_session GET    /members/sign_in(.:format)       {:action=>"new", :controller=>"devise/sessions"}
            member_session POST   /members/sign_in(.:format)       {:action=>"create", :controller=>"devise/sessions"}
    destroy_member_session DELETE /members/sign_out(.:format)      {:action=>"destroy", :controller=>"devise/sessions"}
           member_password POST   /members/password(.:format)      {:action=>"create", :controller=>"devise/passwords"}
       new_member_password GET    /members/password/new(.:format)  {:action=>"new", :controller=>"devise/passwords"}
      edit_member_password GET    /members/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
                           PUT    /members/password(.:format)      {:action=>"update", :controller=>"devise/passwords"}
cancel_member_registration GET    /members/cancel(.:format)        {:action=>"cancel", :controller=>"devise/registrations"}
       member_registration POST   /members(.:format)               {:action=>"create", :controller=>"devise/registrations"}
   new_member_registration GET    /members/sign_up(.:format)       {:action=>"new", :controller=>"devise/registrations"}
  edit_member_registration GET    /members/edit(.:format)          {:action=>"edit", :controller=>"devise/registrations"}
                           PUT    /members(.:format)               {:action=>"update", :controller=>"devise/registrations"}
                           DELETE /members(.:format)               {:action=>"destroy", :controller=>"devise/registrations"}
         new_admin_session GET    /admins/sign_in(.:format)        {:action=>"new", :controller=>"devise/sessions"}
             admin_session POST   /admins/sign_in(.:format)        {:action=>"create", :controller=>"devise/sessions"}
     destroy_admin_session DELETE /admins/sign_out(.:format)       {:action=>"destroy", :controller=>"devise/sessions"}
            admin_password POST   /admins/password(.:format)       {:action=>"create", :controller=>"devise/passwords"}
        new_admin_password GET    /admins/password/new(.:format)   {:action=>"new", :controller=>"devise/passwords"}
       edit_admin_password GET    /admins/password/edit(.:format)  {:action=>"edit", :controller=>"devise/passwords"}
                           PUT    /admins/password(.:format)       {:action=>"update", :controller=>"devise/passwords"}
 cancel_admin_registration GET    /admins/cancel(.:format)         {:action=>"cancel", :controller=>"devise/registrations"}
        admin_registration POST   /admins(.:format)                {:action=>"create", :controller=>"devise/registrations"}
    new_admin_registration GET    /admins/sign_up(.:format)        {:action=>"new", :controller=>"devise/registrations"}
   edit_admin_registration GET    /admins/edit(.:format)           {:action=>"edit", :controller=>"devise/registrations"}
                           PUT    /admins(.:format)                {:action=>"update", :controller=>"devise/registrations"}
                           DELETE /admins(.:format)                {:action=>"destroy", :controller=>"devise/registrations"}
                     films GET    /films(.:format)                 {:action=>"index", :controller=>"films"}
                           POST   /films(.:format)                 {:action=>"create", :controller=>"films"}
                  new_film GET    /films/new(.:format)             {:action=>"new", :controller=>"films"}
                 edit_film GET    /films/:id/edit(.:format)        {:action=>"edit", :controller=>"films"}
                      film GET    /films/:id(.:format)             {:action=>"show", :controller=>"films"}
                           PUT    /films/:id(.:format)             {:action=>"update", :controller=>"films"}
                           DELETE /films/:id(.:format)             {:action=>"destroy", :controller=>"films"}
                      root        /                                {:controller=>"films", :action=>"index"}

的routes.rb

FilmRepo::Application.routes.draw do
  devise_for :members

  devise_for :admins

  resources :films

  root :to => 'films#index'
end

admin.rb(模特)

class Admin < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, and :omniauthable
  devise :database_authenticatable, :registerable, :timeoutable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

member.rb(model)

class Member < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

application.html.haml

!!!
%html
    %head
        %title Film Repo
        = stylesheet_link_tag 'compiled/screen.css', :media => 'screen, projection'
        = stylesheet_link_tag 'compiled/print.css', :media => 'print'
        /[if lt IE 8]
            = stylesheet_link_tag 'compiled/ie.css', :media => 'screen, projection'
            = csrf_meta_tag
    %body.bp
        #container
            #user_nav
                - if member_signed_in?
                    Signed in as #{current_member.email}. Not you?
                    \#{link_to "Sign out", destroy_member_session_path}
                - else
                    = link_to "Sign up", new_member_registration_path
                    or #{link_to "sign in", new_member_session_path}
                - flash.each do |name, msg|
                    = content_tag :div, msg, :id => "flash_#{name}"
            = yield

22 个答案:

答案 0 :(得分:44)

我有类似的问题,但添加了:method =&gt; :删除不起作用。 通过注释掉devise_for:users并添加

,我能够为get请求添加新路由
devise_for :users do
  get '/users/sign_out' => 'devise/sessions#destroy'
end

答案 1 :(得分:42)

您可以通过更改初始化程序中的设备配置来通过get结束会话。

# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get

只需打开该链接即可删除您的会话。

答案 2 :(得分:21)

虽然我不知道原因,但您收到该讯息的原因是因为您的路线中有

destroy_member_session DELETE /members/sign_out(.:format)      {:action=>"destroy", :controller=>"devise/sessions"}

这意味着路由仅适用于DELETE方法而不是GET。这有点奇怪,因为在设计文档中它说应该创建它作为GET路由(https://github.com/plataformatec/devise/blob/master/lib/devise/rails/routes.rb#L30)

将它作为DELETE路由,您应该能够使用

注销
link_to :logout, destroy_member_session_path, :method => :delete 

答案 3 :(得分:18)

我只需要添加

//= require jquery
//= require jquery_ujs

到我的application.js

答案 4 :(得分:16)

我有类似的问题。 我的观看代码是这样的:

  <%= link_to " exit", destroy_user_session_path, method: :delete %>

将以下更改添加到routes.rb之后,

devise_for :users

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

答案 5 :(得分:6)

我们仍然可以在我的代码中使用:method => :delete,就像那样

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

我认为我们无法加载包含jquery的javascript的原因,请确保

= javascript_include_tag "application" (haml- you can use html too)

包含jquery-ui和jquery-ujs。因此,如果它仍然出错,我建议将GEMFILE中的rails gem更改为版本3.2.6并调用bundle update来更新gems。它对我有用!

答案 6 :(得分:5)

在devise.rb中,更改

 config.sign_out_via = :delete

config.sign_out_via = :get

这对我有用。我对此感到疯狂,因为默认值是delete,我不明白为什么。

这有效,但我不确定它是否会影响应用程序中的其他元素。

答案 7 :(得分:5)

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

NOT 工作而不是使用此

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

应该在 application.js

中添加需要 jquery_ujs 的诀窍或更糟糕的情况

答案 8 :(得分:4)

使用Rails4,我不得不使用以下方法:

<%= link_to "Logout", destroy_admin_session_path, method: :delete %>

强调冒号在方法上的位置:和:删除

答案 9 :(得分:3)

也许这会对某人有所帮助。 从Rails 3.0升级到3.1并发现了这个问题。 这为我解决了这个问题:

routes.rb中:
    devise_for:users

devise.rb:
    config.sign_out_via =:删除

application.html.erb:

<%= javascript_include_tag "application" %>     

* 不是:默认

_login_items.html.erb:

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

app / assets / javascripts / application.js

//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree .

我在javascript / jquery.js,jquery_ujs.js中已经删除了3.0版本。

答案 10 :(得分:3)

您可能已删除资产/ javascripts / *

运行rails generate jquery:install --ui这将生成所有javascripts,如下所示

xxxx@xxxxx:~/Projects/Rails_apps/rtest$ rails generate jquery:install --ui
      remove  public/javascripts/prototype.js
      remove  public/javascripts/effects.js
      remove  public/javascripts/dragdrop.js
      remove  public/javascripts/controls.js
     copying  jQuery (1.7.1)
      create  public/javascripts/jquery.js
      create  public/javascripts/jquery.min.js
     copying  jQuery UI (1.8.16)
      create  public/javascripts/jquery-ui.js
      create  public/javascripts/jquery-ui.min.js
     copying  jQuery UJS adapter (822920)
      remove  public/javascripts/rails.js
      create  public/javascripts/jquery_ujs.js

转到您的布局,例如application.html.erb并修改<%= javascript_include_tag :all %>

这对我有用:)

答案 11 :(得分:2)

@creamhost说,

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

但对我来说这不是正确的解决方案(Rails 4)。我解决了我们的问题(@Olives的答案),

link_to :logout, destroy_member_session_path, method: :delete

答案 12 :(得分:2)

如果出于安全原因要使用:delete方法并且不依赖于jquery-ujs,则可以使用button_to代替link_to,例如:

button_to "Log out", destroy_user_session_path, method: :delete

如果使用link_to,则必须确保启用了JavaScript:

请注意,如果用户禁用了JavaScript,则请求将落空 回到使用GET。

docs

答案 13 :(得分:2)

FWIW我也遇到过这个问题。已经查看了所有建议的答案,但唯一有效的方法是打开routes.rb并注释掉以下行:

devise_for :users

在下面,添加以下行:

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

答案 14 :(得分:2)

问题始于assets/javascript/中的rails 3.1。只需查找application.js,如果该文件不存在,请创建一个具有该名称的文件。我不知道为什么我的文件消失或者从未在rails new app上创建...该文件是jquery的实例。

答案 15 :(得分:1)

只需使用以下内容进行退出链接:

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

答案 16 :(得分:1)

//= require jquery_ujs

您的资产中缺少此行。无需获得/users/signout请求。将此行放在页面顶部的JavaScript文件中。

答案 17 :(得分:1)

我正在使用Rails版本5。我也遇到了这个问题。我所做的简单修复是将初始化中的devise配置更改为默认值。

发件人:

config.sign_out_via = :get

收件人:

config.sign_out_via = :delete

答案 18 :(得分:0)

我得到了OP对/ users / sign_out的相同错误,因为我正在使用它而不是删除它(我也是第一年开发)。通过这个答案检查github后,我提交了拉取请求来纠正它!

https://github.com/plataformatec/devise/pull/2040

Go S.O. !!!

答案 19 :(得分:0)

有同样的问题并且记得它只是在我决定“清理”我的Javascript文件后才开始发生。所以我再次运行rails generate jquery:install --ui,这为我解决了。 (如果你不需要JQuery UI,我可以忽略--ui部分。)

答案 20 :(得分:0)

使用正确的 HTTP 方法保留您的 devise.rb

# good 
config.sign_out_via = :delete

# bad
config.sign_out_via = :get

使用 button_to 而不是 link_to

# good 
= button_to "Sign Out", destroy_user_session_path, method: :delete

# bad
= link_to "Sign Out", destroy_user_session_path, method: :delete"

如果您使用引导程序(保持优雅)

= link_to "Sign Out", destroy_user_session_path, method: :delete, class: "btn btn-default btn-sm"

参考:github.com/heartcombo/devise/issues/4570#issuecomment-740812109

答案 21 :(得分:-2)

仅在Windows上发生..将以下内容添加到Application.html.erb文件中。

devise_for:users

devise_scope:用户执行    得到&#39; / users / sign_out&#39; =&GT; &#39;设计/会话#摧毁&#39;