【问题标题】:Devise Helpers (authenticate_user!, current_user, user_signed_in?) not initialized设计助手(authenticate_user!,current_user,user_signed_in?)未初始化
【发布时间】:2013-05-23 23:39:29
【问题描述】:

我正在设置设计并且需要能够使用设计助手。 当我将before_filter :authenticate_user! 添加到应用程序控制器中时,我收到以下错误undefined method 'authenticate_user!' for #<HomeController:*>

它不直接存在于我的家庭控制器中,而是从应用程序控制器继承,如果我在我的家庭(或任何控制器)中使用 skip_before_filter :authenticate_user! 并尝试访问该页面,我会收到以下错误。

undefined method `user_signed_in?' for #<#<Class *>

它在 layouts/application.html.erb 文件中调用它的那一行中断。任何助手都会发生同样的事情。看起来好像根本没有加载它们,但是,当我加载 Rails 控制台并输入 $LOAD_PATH.dup 时,输出包含

 "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/controllers", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/helpers", "/usr/local/rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/app/mailers" 

看来它们确实应该由导轨加载。

这是我的应用程序控制器

类 ApplicationController

helper :all # 一直包含所有助手
防止伪造
包括 ActionView::Helpers::NumberHelper
before_filter :authenticate_user!

结束

我确实在安装设备后重新启动了服务器,并且多次尝试不同的尝试来解决问题。 如果需要更多文件或信息,请告诉我。非常感谢您提前。

编辑:我的 user.rb 文件被要求显示我确实安装了设计

class User < ActiveRecord::Base

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :encryptable, :encryptor => :restful_authentication_sha1  

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

end

编辑:整个错误信息

HomeController#index 中的 NoMethodError

未定义的方法 `authenticate_user!'对于 # '

这是完整的跟踪

activesupport (3.2.13) lib/active_support/callbacks.rb:418:in `run_2094683035699451847__process_action_2229546931289158113_callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'

activesupport (3.2.13) lib/active_support/callbacks.rb:385:in >`_run_process_action_callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'

actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'

actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'

actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'

activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'

activesupport (3.2.13) lib/active_support/notifications/instrument.rb:20:in `instrument'

activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'

actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'

actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'

actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'

actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'

actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'

actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'

actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'

journey (1.0.4) lib/journey/router.rb:68:in `block in call'

journey (1.0.4) lib/journey/router.rb:56:in `each'

journey (1.0.4) lib/journey/router.rb:56:in `call'

actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'

warden (1.2.1) lib/warden/manager.rb:35:in `block in call'

warden (1.2.1) lib/warden/manager.rb:34:in `catch'

warden (1.2.1) lib/warden/manager.rb:34:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'

rack (1.4.5) lib/rack/etag.rb:23:in `call'

rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'

机架 (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'

rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'

activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'

activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'

activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `run_177144612565476380_call_4534346825225857812__callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'

activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'

activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'

actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'

railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'

railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'

activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'

railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'

rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'

rack (1.4.5) lib/rack/runtime.rb:17:in `call'

activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'

rack (1.4.5) lib/rack/lock.rb:15:in `call'

actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'

railties (3.2.13) lib/rails/engine.rb:479:in `call'

railties (3.2.13) lib/rails/application.rb:223:in `call'

railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'

passenger (4.0.2) lib/phusion_passenger/rack/thread_handler_extension.rb:77:in `process_request'

passenger (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:135:in `accept_and_process_next_request'

passenger (4.0.2) lib/phusion_passenger/request_handler/thread_handler.rb:106:in `main_loop'

passenger (4.0.2) lib/phusion_passenger/request_handler.rb:449:in `block (4 levels) in start_threads'

passenger (4.0.2) lib/phusion_passenger/utils/robust_interruption.rb:108:in `disable_interruptions'

passenger (4.0.2) lib/phusion_passenger/request_handler.rb:444:in `block (3 levels) in start_threads'

编辑:这是在控制台中运行 Devise.helpers 的文本。

$ rails 控制台

加载开发环境(Rails 3.2.13)

1.9.3-p429 :001 > Devise.helpers

=> #

编辑:这是我的路由 rb 文件。 :Application.routes.draw 做

devise_for :users  

resource :sessions, :only => [:new, :create, :destroy]

devise_scope :user do
    match 'signup' => 'users#new', :as => :signup
    match 'register' => 'users#create', :as => :register
    match '/login' => 'sessions#new', :as => :login 
     match 'logout' => 'sessions#destroy', :as => :logout
end
match '/activate/:activation_code' => 'users#activate', :as => :activate, :activation_code => nil

match '/users/:id', :to => 'users#show', :as => :user

resources :users do

member do
     put :suspend
     put :unsuspend
     delete :purge
end

end

resource :sessions

   other resources

match '' => 'home#index', :as => :home
match ':controller(/:action(/:id))'
root to: 'home#index'
end 

【问题讨论】:

  • @Btuman,尝试在 routes.rb 中尝试 devise_for 可能在路由设置/命名空间问题中存在错误。尝试rake routes 看看它们是否设置正确。我之前遇到过这样的问题,它与错误的路线设置直接相关
  • @RickCigarette,为什么助手会受到不良路线的影响?

标签: ruby-on-rails ruby-on-rails-3 devise


【解决方案1】:

请务必在您的routes.rb 中添加devise_for :user

在此处查看更多信息:https://stackoverflow.com/a/11180576

【讨论】:

  • 这个答案在仅 API 的应用程序中对我有用。我必须在根级别添加devise_for :user 才能定义authenticate_user! 方法,即使我只需要api/v1 命名空间中的devise_for :user
  • @pdobb:你把它们都保留了吗? (root 级别和api/v1)?还是您只将devise_for 放在root 级别?
  • @Throoze 我相信我必须在两个地方都有它。至少,现在回想起来就是我现在所拥有的,但我现在不记得细节了。
  • 如果使用命名空间,您似乎需要在根级别使用它来让助手工作,然后在您的命名空间中使用,这样您就可以获得所需的路由。
  • 给这个答案一个大的绿色检查!
【解决方案2】:

只需重新启动您的 Rails 服务器。

“请注意,如果您已经启动了应用程序,则应在此处重新启动它。 否则你会遇到奇怪的错误,比如用户无法登录和路由助手未定义。” [入门说明]

[编辑] 更详细的答案:

请验证另外两件事:您的 HomeController 应该继承自 Devise::Controllers::Helpers。 route.rb 中的 devise 指令(在 Helpers 中生成缺失的方法)应该扩展了 Devise.@@mappings。下面的代码可以检查这一点:

class ApplicationController ...
  ...
  before_filter do
    fail "bad ancestor" unless self.kind_of?(Devise::Controllers::Helpers)
    fail "no mapping" unless Devise.class_variable_get(:@@mappings).[:user]
    authenticate_user!
  end
end

要在工作的 rails 应用程序中引入类似的问题,必须禁用 config/initializers/devise.rb 和 config/routes.rb 设计条目,并且必须重新启动 rails。除非重新启动 rails,否则重新启用代码不会解决问题。

【讨论】:

  • “我在安装设计后确实重新启动了服务器,并且多次尝试不同的尝试来解决问题”引用上述问题。
  • 这证实了我的 ApplicationController 不是从 Devise::Controllers::Helpers 继承的。现在我必须弄清楚为什么。
【解决方案3】:

有很多事情会出错。需要按顺序检查的一些事项:

  • 无法调用before_filter :authenticate_user! 表示设备设置不正确。
  • 在 routes.rb 中添加devise_for :users(它是用户s,你没看错),接下来简化这些作用域规则进行测试。
  • 您需要让 before_filter :authenticate_user! 工作以在控制器中获取助手或手动包含助手
  • 如果您的基本控制器没有从 ActionController 继承,那么您需要手动添加辅助方法:将其放入您的基本控制器 helper_method "current_user", "user_signed_in?", "user_session"
  • 要检查的一件事:self.controller.current_user 在您的 erb 中有效吗?这表明没有包含 helper_methods。

【讨论】:

    【解决方案4】:

    我将这一行添加到我的控制器中,这对我有用:

    include Devise::Controllers::Helpers
    

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题(Rails 5.1.2,Devise 4.3.0),碰巧我正在使用:-

      devise_for :user do
        ...
      end
      

      而不是

      devise_for :users do
        ...
      end
      

      我通过更改命名空间来修复它。

      用户 => 用户

      【讨论】:

        【解决方案6】:

        按照 Devise 的入门指南,我遇到了同样的问题。

        原来我错误地运行了rails generate model User 而不是rails generate devise User,这只是创建了一个恰好名为“用户”的普通 ActiveRecord 模型。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-05-12
          • 2011-09-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多