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

时间:2013-05-28 21:10:22

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

我正在设置设计,需要能够使用设计助手。 当我将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" 

所以它们确实应该被rails加载。

这是我的应用程序控制器

  

class ApplicationController&lt; ActionController :: Base

     
    

helper:所有#包括所有帮助者,所有时间
    protect_from_forgery
    包括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中的NoMethodError #index

     

未定义的方法`authenticate_user!&#39;对于#&lt; HomeController:0x000000040c45a0&gt; &#39;

这是完整的痕迹

  

activesupport(3.2.13)lib / active_support / callbacks.rb:418:在`运行 _2094683035699451847__process_action_ 2229546931289158113 _callbacks&#39;

     

activesupport(3.2.13)lib / active_support / callbacks.rb:405:在`__run_callback&#39;

     

activesupport(3.2.13)lib / active_support / callbacks.rb:385:in&gt;`_run_process_action_callbacks&#39;

     

activesupport(3.2.13)lib / active_support / callbacks.rb:81:在`run_callbacks&#39;

     

actionpack(3.2.13)lib / abstract_controller / callbacks.rb:17:在`process_action&#39;

     

actionpack(3.2.13)lib / action_controller / metal / rescue.rb:29:在`process_action&#39;

     

actionpack(3.2.13)lib / action_controller / metal / instrumentation.rb:30:在`block in process_action&#39;

     

activesupport(3.2.13)lib / active_support / notifications.rb:123:在`block in instrument&#39;

     

activesupport(3.2.13)lib / active_support / notifications / instrumenter.rb:20:在`instrument&#39;

     

activesupport(3.2.13)lib / active_support / notifications.rb:123:在`instrument&#39;

     

actionpack(3.2.13)lib / action_controller / metal / instrumentation.rb:29:在`process_action&#39;

     

actionpack(3.2.13)lib / action_controller / metal / params_wrapper.rb:207:在`process_action&#39;

     

actionpack(3.2.13)lib / abstract_controller / base.rb:121:在`process&#39;

     

actionpack(3.2.13)lib / abstract_controller / rendering.rb:45:在`process&#39;

     

actionpack(3.2.13)lib / action_controller / metal.rb:203:在`dispatch&#39;

     

actionpack(3.2.13)lib / action_controller / metal / rack_delegation.rb:14:在`dispatch&#39;

     

actionpack(3.2.13)lib / action_controller / metal.rb:246:在`block in action&#39;

     

actionpack(3.2.13)lib / action_dispatch / routing / route_set.rb:73:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / routing / route_set.rb:73:在`dispatch&#39;

     

actionpack(3.2.13)lib / action_dispatch / routing / route_set.rb:36:在`call&#39;

     

旅程(1.0.4)lib / journey / router.rb:68:在`阻止通话&#39;

     

旅程(1.0.4)lib / journey / router.rb:56:在每个&#39;

     

旅程(1.0.4)lib / journey / router.rb:56:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / routing / route_set.rb:612:在`call&#39;

     

warden(1.2.1)lib / warden / manager.rb:35:in block in call&#39;

     

warden(1.2.1)lib / warden / manager.rb:34:in catch&#39;

     

warden(1.2.1)lib / warden / manager.rb:34:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / best_standards_support.rb:17:在`call&#39;

     

rack(1.4.5)lib / rack / etag.rb:23:在`call&#39;

     

rack(1.4.5)lib / rack / conditionalget.rb:25:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / head.rb:14:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / params_parser.rb:21:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / flash.rb:242:在`call&#39;

     

rack(1.4.5)lib / rack / session / abstract / id.rb:210:在`context&#39;

     

rack(1.4.5)lib / rack / session / abstract / id.rb:205:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / cookies.rb:341:在`call&#39;

     

activerecord(3.2.13)lib / active_record / query_cache.rb:64:在`call&#39;

     

activerecord(3.2.13)lib / active_record / connection_adapters / abstract / connection_pool.rb:479:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / callbacks.rb:28:in block in call&#39;

     

activesupport(3.2.13)lib / active_support / callbacks.rb:405:在`运行 _177144612565476380_ 致电 _4534346825225857812__callbacks&#39;

     

activesupport(3.2.13)lib / active_support / callbacks.rb:405:在`__run_callback&#39;

     

activesupport(3.2.13)lib / active_support / callbacks.rb:385:在`_run_call_callbacks&#39;

     

activesupport(3.2.13)lib / active_support / callbacks.rb:81:在`run_callbacks&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / callbacks.rb:27:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / reloader.rb:65:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / remote_ip.rb:31:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / debug_exceptions.rb:16:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / show_exceptions.rb:56:在`call&#39;

     

railties(3.2.13)lib / rails / rack / logger.rb:32:在`call_app&#39;

     

railties(3.2.13)lib / rails / rack / logger.rb:16:在`block in call&#39;

     

activesupport(3.2.13)lib / active_support / tagged_logging.rb:22:在`已标记&#39;

     

railties(3.2.13)lib / rails / rack / logger.rb:16:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / request_id.rb:22:在`call&#39;

     

rack(1.4.5)lib / rack / methodoverride.rb:21:在`call&#39;

     

rack(1.4.5)lib / rack / runtime.rb:17:在`call&#39;

     

activesupport(3.2.13)lib / active_support / cache / strategy / local_cache.rb:72:在`call&#39;

     

rack(1.4.5)lib / rack / lock.rb:15:在`call&#39;

     

actionpack(3.2.13)lib / action_dispatch / middleware / static.rb:63:在`call&#39;

     

railties(3.2.13)lib / rails / engine.rb:479:在`call&#39;

     

railties(3.2.13)lib / rails / application.rb:223:在`call&#39;

     

railties(3.2.13)lib / rails / railtie / configurable.rb:30:在`method_missing&#39;

     

passenger(4.0.2)lib / phusion_passenger / rack / thread_handler_extension.rb:77:在`process_request&#39;

     

passenger(4.0.2)lib / phusion_passenger / request_handler / thread_handler.rb:135:在`accept_and_process_next_request&#39;

     

passenger(4.0.2)lib / phusion_passenger / request_handler / thread_handler.rb:106:在`main_loop&#39;

     

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

     

passenger(4.0.2)lib / phusion_passenger / utils / robust_interruption.rb:108:在`disable_interruptions&#39;

     

passenger(4.0.2)lib / phusion_passenger / request_handler.rb:444:在start_threads中的“block(3个级别)&#39;

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

  

$ rails console

     

加载开发环境(Rails 3.2.13)

     

1.9.3-p429:001&gt; Devise.helpers

     

=&GT; #&LT;设置:{Devise :: Controllers :: Helpers}&gt;

编辑:这是我的路由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 

4 个答案:

答案 0 :(得分:32)

请务必在devise_for :user

中添加routes.rb

在此处查看更多内容:https://stackoverflow.com/a/11180576

答案 1 :(得分:2)

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

  • 无法呼叫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_method。

答案 2 :(得分:1)

我将这一行添加到我的控制器中,为我做了诀窍:

include Devise::Controllers::Helpers

答案 3 :(得分:0)

在Devise的入门指南之后,我遇到了同样的问题。

原来我错误地运行了rails generate model User而不是rails generate devise User,它刚刚创建了一个简单的ActiveRecord模型,其名称恰好是&#39; User&#39;。