Rails应用程序在javascript中找不到'destroy_user_session_path'

时间:2012-02-12 17:01:57

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

我有一个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) %>

的application.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

Rails 3.2.0 ruby 1.8.7(2011-02-18 patchlevel 334)[i686-linux]

  • rails(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

4 个答案:

答案 0 :(得分:6)

如果您想在没有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的理由:

答案 1 :(得分:3)

我错过了添加路径的数据库authenticatable devise模块。

应用程序/模型/ user.rb:

 devise :omniauthable, :database_authenticatable

rake routes:

      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

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

答案 2 :(得分:2)

你有两个解决方案:

  1. 添加:method =&gt; :删除link_to
  2. 或者,在devise.rb文件中更改行config.sign_out_via = :删除到config.sign_out_via =:get

答案 3 :(得分:1)

user_session_path(current_user), :method => :delete

除非你有一个特定的命名路线“毁灭”,否则我的建议是