Ruby on Rails中的NoMethodError从控制器调用Class方法

时间:2011-05-03 20:43:05

标签: ruby-on-rails ruby

我只是在学习RoR并遇到了我的第一个真正的bug。真的很感激它的一些指导。我正在为我正在构建的站点创建用户登录/注销功能,这将通过会话进行维护。当登录表单提交时,它将转到调用“create”方法的会话控制器。当它执行此操作时,它会显示以下错误:

  

NoMethodError in   SessionsController#创建

     

当你没有时,你有一个零对象   期待它!你可能已经预料到了   ActiveRecord :: Base的实例。该   评估nil。[]

时发生错误

以下是create方法的代码:

  33 def create
  34   user = User.authenticate(params[:session][:email],
  35                           params[:session][:password])
  36   if user.nil?
  37     flash.now[:error] = "Invalid email/password combination."
  38     @title = "Sign in"
  39     render 'new'
  40   else
  41     sign_in user
  42     redirect_back_or user
  43   end
  44 end

应用程序跟踪说错误在

app / controllers / sessions_controller.rb:34:在'create'

所以这个问题似乎与authenticate方法有关。这是为我的用户对象定义的类方法。有点奇怪的是,我在rails控制台中测试了这个方法并且工作正常。以下是完整的跟踪。再次非常感谢对此的一些帮助。谢谢你。

跟踪:

  

应用程序/控制器/ sessions_controller.rb:34:在   create' actionpack (3.0.4) lib/action_controller/metal/implicit_render.rb:4:in send_action'actionpack(3.0.4)   LIB / abstract_controller / base.rb:150:在   process_action' actionpack (3.0.4) lib/action_controller/metal/rendering.rb:11:in process_action'actionpack(3.0.4)   LIB / abstract_controller / callbacks.rb:18:在   block in process_action' activesupport (3.0.4) lib/active_support/callbacks.rb:435:in 运行 _4224187041876590211__process_action_ 3718750575726612430 _callbacks'   activesupport(3.0.4)   LIB / active_support / callbacks.rb:409:在   _run_process_action_callbacks' activesupport (3.0.4) lib/active_support/callbacks.rb:93:in run_callbacks'actionpack(3.0.4)   LIB / abstract_controller / callbacks.rb:17:在   process_action中的process_action' actionpack (3.0.4) lib/action_controller/metal/instrumentation.rb:30:in 阻止   activesupport(3.0.4)   LIB / active_support / notifications.rb:52:在   block in instrument' activesupport (3.0.4) lib/active_support/notifications/instrumenter.rb:21:in instrument'activesupport(3.0.4)   LIB / active_support / notifications.rb:52:在   instrument' actionpack (3.0.4) lib/action_controller/metal/instrumentation.rb:29:in process_action'actionpack(3.0.4)   LIB / action_controller /金属/ rescue.rb:17:在   process_action' actionpack (3.0.4) lib/abstract_controller/base.rb:119:in 进程'actionpack(3.0.4)   LIB / abstract_controller / rendering.rb:41:在   process' actionpack (3.0.4) lib/action_controller/metal.rb:138:in dispatch'actionpack(3.0.4)   LIB / action_controller /金属/ rack_delegation.rb:14:在   dispatch' actionpack (3.0.4) lib/action_controller/metal.rb:178:in 阻止行动'actionpack(3.0.4)   LIB / action_dispatch /路由/ route_set.rb:62:在   call' actionpack (3.0.4) lib/action_dispatch/routing/route_set.rb:62:in dispatch'actionpack(3.0.4)   LIB / action_dispatch /路由/ route_set.rb:27:在   call' rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in 阻止呼叫'机架安装(0.6.14)   LIB /架/安装/ code_generation.rb:93:在   block in recognize' rack-mount (0.6.14) lib/rack/mount/code_generation.rb:68:in optimized_each'机架式安装(0.6.14)   LIB /架/安装/ code_generation.rb:92:在   recognize' rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in 调用'actionpack(3.0.4)   LIB / action_dispatch /路由/ route_set.rb:492:在   call' actionpack (3.0.4) lib/action_dispatch/middleware/best_standards_support.rb:17:in 调用'actionpack(3.0.4)   LIB / action_dispatch /中间件/ head.rb:14:在   call' rack (1.2.2) lib/rack/methodoverride.rb:24:in 调用'actionpack(3.0.4)   LIB / action_dispatch /中间件/ params_parser.rb:21:在   call' actionpack (3.0.4) lib/action_dispatch/middleware/flash.rb:182:in 调用'actionpack(3.0.4)   LIB / action_dispatch /中间件/会话/ abstract_store.rb:149:在   call' actionpack (3.0.4) lib/action_dispatch/middleware/cookies.rb:302:in调用'activerecord(3.0.4)   LIB / active_record / query_cache.rb:32:在   block in call' activerecord (3.0.4) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in 缓存'activerecord(3.0.4)   LIB / active_record / query_cache.rb:12:在   cache' activerecord (3.0.4) lib/active_record/query_cache.rb:31:in 调用'activerecord(3.0.4)   LIB / active_record / connection_adapters /抽象/ connection_pool.rb:354:在   call' actionpack (3.0.4) lib/action_dispatch/middleware/callbacks.rb:46:in 阻止来电'activesupport(3.0.4)   LIB / active_support / callbacks.rb:415:在   _run_call_callbacks' actionpack (3.0.4) lib/action_dispatch/middleware/callbacks.rb:44:in 呼叫'机架(1.2.2)   lib / rack / sendfile.rb:107:在call' actionpack (3.0.4) lib/action_dispatch/middleware/remote_ip.rb:48:in 调用'actionpack(3.0.4)   LIB / action_dispatch /中间件/ show_exceptions.rb:47:在   call' railties (3.0.4) lib/rails/rack/logger.rb:13:in称之为”   rack(1.2.2)lib / rack / runtime.rb:17:in   call' activesupport (3.0.4) lib/active_support/cache/strategy/local_cache.rb:72:in 呼叫'机架(1.2.2)   lib / rack / lock.rb:11:in block in call' <internal:prelude>:10:in synchronize'   rack(1.2.2)lib / rack / lock.rb:11:in   call' actionpack (3.0.4) lib/action_dispatch/middleware/static.rb:30:in 称'铁路(3.0.4)   lib / rails / application.rb:168:in call' railties (3.0.4) lib/rails/application.rb:77:in method_missing'railties(3.0.4)   LIB /导轨/机架/ log_tailer.rb:14:   call' rack (1.2.2) lib/rack/content_length.rb:13:in 呼叫'机架(1.2.2)   LIB /架/处理器/ webrick.rb:52:在   service' /Users/USERNAME_REMOVED/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/httpserver.rb:111:in 服务”   /Users/USERNAME_REMOVED/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/httpserver.rb:70:in   {_ 1}}阻止在start_thread'

2 个答案:

答案 0 :(得分:2)

params[:session]不存在

def create
  user = User.authenticate(params[:session][:email],
                        params[:session][:password]) if params[:session]
   ...

答案 1 :(得分:0)

而不是在authenticate中声明方法class << selfclass << self def authenticate(email, submitted_password) user = find_by_email(email) (user && user.has_password?(submitted_password)) ? user : nil end end

试试这个:def self.authenticate(email, submitted_password)。要小心,你必须从class << self

声明它

在这种情况下,您声明了一个类方法authenticate,您应该能够在create方法中在控制器中调用它。