【问题标题】:Rails app cannot find 'destroy_user_session_path' in javascriptRails 应用程序在 javascript 中找不到“destroy_user_session_path”
【发布时间】:2012-02-12 17:01:56
【问题描述】:

我有一个 Rails 应用程序,多年来从 2.2 迁移到 3.2。我正在使用 Devise 和 Omniauth 进行登录,不幸的是,我认为在迁移过程中我搞砸了。

当我尝试在 application.html.erb 中设置会话删除时,我收到以下错误:

undefined local variable or method `destroy_user_session_path' for #<#<Class:0xb468e278>:0xb423e1dc>

application.html.erb:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>Coffee Tracker</title>
  <%= stylesheet_link_tag 'application'%>
  <!--%= stylesheet_link_tag 'scaffold' %-->
</head>
<!--%= javascript_include_tag :defaults %-->
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tag %>
...snip...
<% if user_signed_in? %>
  <span id="currentuser"><%= current_user.email %></span>
  <%= link_to('Logout', destroy_user_session_path, :method => 'delete') %>
<% else %>

当我尝试这条线时,我也得到了同样的错误:

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

应用程序.js

// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//= require_self
//= require_tree .
//= require jquery
//= require jquery_ujs

导轨 3.2.0 ruby 1.8.7 (2011-02-18 补丁级别 334) [i686-linux]

  • 导轨 (3.2.0)
  • jquery-rails (2.0.0)
  • 设计 (2.0.0)
  • omniauth (1.0.2)
  • omniauth-facebook (1.2.0)
  • omniauth-oauth2 (1.0.0)

更新: 这是 rake 路线:

user_omniauth_callback     /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:facebook)
           coffee_list GET /coffee/list(.:format)                 coffee#list
                  root     /                                      menu#index
                           /:controller(/:action(/:id(.:format))) :controller#:action

【问题讨论】:

  • 如果您对routes.rb文件进行任何更改,您必须重新启动网络服务器才能看到更改。
  • 你能做一个 rake routes 并发布与用户会话相关的部分吗?

标签: javascript ruby-on-rails ruby devise ruby-on-rails-3.2


【解决方案1】:

如果你想使用omniauthable而不使用database_authenticatable,你需要手动添加路由。

routes.rb:

devise_scope :user do
  delete "/users/sign_out" => "devise/sessions#destroy"
end

那么在你看来:

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

有关详细信息,请参阅以下 Omniauth Facebook 和 Twitter 教程:

为什么我们必须这样做?在此处查看 Jose Valim 的基本原理:

【讨论】:

  • 没问题 :) 我很高兴别人能从我的奋斗中受益
【解决方案2】:

我错过了添加路由的数据库可验证设计模块。

app/models/user.rb:

 devise :omniauthable, :database_authenticatable

耙路线:

      new_user_session GET    /users/sign_in(.:format)               devise/sessions#new
          user_session POST   /users/sign_in(.:format)               devise/sessions#create
  destroy_user_session DELETE /users/sign_out(.:format)              devise/sessions#destroy
user_omniauth_callback        /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:facebook)
           coffee_list GET    /coffee/list(.:format)                 coffee#list
                  root        /                                      menu#index
                              /:controller(/:action(/:id(.:format))) :controller#:action

我发现文档确认显示使用可验证的模块在 devise_for 文档中创建这些路由:http://rubydoc.info/github/plataformatec/devise/master/ActionDispatch/Routing/Mapper#devise_for-instance_method

【讨论】:

  • 您的用户是否也在对本地数据库进行身份验证(与omniauth 方法相反)?我正在使用类似的设置,我不需要模型的所有extra functionality that database_authenticatable adds。我想知道是否只手动添加路线会更好? delete '/users/auth/sign_out', :to =&gt; 'users/sessions#destroy', :as =&gt; :destroy_user_session
  • 实际上是的。我让他们最初能够在本地或通过omniauth 登录。
【解决方案3】:

您有两种解决方案:

  1. 将 :method => :delete 添加到 link_to
  2. 或者,在 devise.rb 文件中更改行 config.sign_out_via = :delete to config.sign_out_via = :get

【讨论】:

  • 对不起,问题中有错字,我确实尝试了 :method => :delete ,但仍然失败。
  • 你试过 config.sign_out_via = :get in the devise.rb 文件吗?
  • 是的,它不起作用,因为我没有使用添加路由的可验证模型(即,我的模型中的 database_authenticable)。
【解决方案4】:
user_session_path(current_user), :method => :delete

这是我的建议,除非你有一个特定的命名路线“破坏”

【讨论】:

  • destroy_user_session_path 应该是一条路由吗?我想我的部分问题是我不知道它应该在哪里/如何生成,所以我什至不确定在哪里修复它。
猜你喜欢
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 2014-07-04
  • 2016-07-24
  • 1970-01-01
相关资源
最近更新 更多