当前位置:  开发笔记 > 编程语言 > 正文

Devise Helpers(authenticate_user!,current_user,user_signed_in?)未初始化

如何解决《DeviseHelpers(authenticate_user!,current_user,user_signed_in?)未初始化》经验,为你挑选了2个好方法。

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

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

undefined method `user_signed_in?' for #<#

它打破了调用它的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" 

所以看起来它们确实应该由rails加载.

这是我的应用程序控制器

class ApplicationController

helper:all
#include all helper,protect_from_forgery一直
包括ActionView :: Helpers :: NumberHelper
before_filter:authenticate_user!

结束

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

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

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中的NoMethodError #index

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

这是完整的痕迹

activesupport(3.2.13)lib/active_support/callbacks.rb:418:在` run _2094683035699451847__process_action_ 2229546931289158113 _callbacks'中

activesupport(3.2.13)lib/active_support/callbacks.rb:405:在`__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:在`run_callbacks'中

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

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

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

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

activesupport(3.2.13)lib/active_support/notifications/instrumenter.rb:20:in"instrument"

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

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

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

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

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

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

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

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

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

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

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

旅程(1.0.4)lib/journey/router.rb:68:在'阻塞中'

旅程(1.0.4)lib/journey/router.rb:56:in"each"

旅程(1.0.4)lib/journey/router.rb:56:在`call'

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

warden(1.2.1)lib/warden/manager.rb:35:在`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:在`call'中

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

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

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

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

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

rack(1.4.5)lib/rack/session/abstract/id.rb:210:在`context'中

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

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

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

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

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

activesupport(3.2.13)lib/active_support/callbacks.rb:405:在` run _177144612565476380_ call _4534346825225857812__callbacks'

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

passenger(4.0.2)lib/phusion_passenger/request_handler.rb:449:在start_threads中的`block(4个级别)'

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

passenger(4.0.2)lib/phusion_passenger/request_handler.rb:444:在start_threads中的'block(3个级别)'

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

$ rails console

加载开发环境(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 

paulguy.. 32

一定要加入devise_for :user你的routes.rb

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



1> paulguy..:

一定要加入devise_for :user你的routes.rb

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


这个答案适用于我的API应用程序.我必须在根级别添加`devise_for:user`以便定义`authenticate_user!`方法,即使我在`api/v1`命名空间中只需要`devise_for:user`.
给这个答案一个大绿色检查!

2> kunysch..:

只需重启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

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

推荐阅读
李桂平2402851397
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有