NoMethodError的完整跟踪

这基本上是我之前问题的第2部分,因为我已经根据代码的长度创建了一个新问题。 希望我们能够在您的帮助下解决这个问题,我相信我们现在可以通过新信息找到解决方案。

编辑:感谢您的输入。 这是pt的链接。 1与实际代码: 登录function不起作用 。

PS。 请不要忘记投票这个问题。

inheritance人user.rb:

# == Schema Information # # Table name: users # # id :integer not null, primary key # name :string(255) # email :string(255) # created_at :datetime not null # updated_at :datetime not null # encrypted_password :string(255) # salt :string(255) # class User  true, :length => { :maximum => 50 } validates :email, :presence => true, :format => { :with => email_regex }, :uniqueness => { :case_sensitive => false } validates :password, :presence => true, :confirmation => true, :length => { :within => 6..40 } before_save :encrypt_password def has_password?(submitted_password) encrypted_password == encrypted(submitted_password) end class << self def User.authenticate(email, submitted_password) user = find_by_email(email) (user && user.has_password?(submitted_password)) ? user : nil end def authenticate_with_salt(id, cookie_salt) user = find_by_id(id) (user && user.salt == cookie_salt) ? user : nil end end private def encrypt_password self.salt = make_salt if new_record? self.encrypted_password = encrypt(password) end def encrypt(string) secure_hash("#{salt}--#{string}") end def make_salt secure_hash("#{Time.now.utc}--#{password}") end def secure_hash(string) Digest::SHA2.hexdigest(string) end end 

和最初的完整跟踪:

 undefined method `encrypted' for # Rails.root: /Users/anthonypanepinto/Sites/rails_projects/sample_app1 Application Trace | Framework Trace | Full Trace activemodel (3.2.11) lib/active_model/attribute_methods.rb:407:in `method_missing' activerecord (3.2.11) lib/active_record/attribute_methods.rb:149:in `method_missing' app/models/user.rb:32:in `has_password?' app/models/user.rb:38:in `authenticate' app/controllers/sessions_controller.rb:7:in `create' actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:4:in `send_action' actionpack (3.2.11) lib/abstract_controller/base.rb:167:in `process_action' actionpack (3.2.11) lib/action_controller/metal/rendering.rb:10:in `process_action' actionpack (3.2.11) lib/abstract_controller/callbacks.rb:18:in `block in process_action' activesupport (3.2.11) lib/active_support/callbacks.rb:414:in `_run__1623305808682769517__process_action__2997016911314885603__callbacks' activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback' activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks' activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks' actionpack (3.2.11) lib/abstract_controller/callbacks.rb:17:in `process_action' actionpack (3.2.11) lib/action_controller/metal/rescue.rb:29:in `process_action' actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' activesupport (3.2.11) lib/active_support/notifications.rb:123:in `block in instrument' activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in `instrument' activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument' actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:29:in `process_action' actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:207:in `process_action' activerecord (3.2.11) lib/active_record/railties/controller_runtime.rb:18:in `process_action' actionpack (3.2.11) lib/abstract_controller/base.rb:121:in `process' actionpack (3.2.11) lib/abstract_controller/rendering.rb:45:in `process' actionpack (3.2.11) lib/action_controller/metal.rb:203:in `dispatch' actionpack (3.2.11) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' actionpack (3.2.11) lib/action_controller/metal.rb:246:in `block in action' actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `call' actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `dispatch' actionpack (3.2.11) 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.11) lib/action_dispatch/routing/route_set.rb:601:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' rack (1.4.4) lib/rack/etag.rb:23:in `call' rack (1.4.4) lib/rack/conditionalget.rb:35:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call' rack (1.4.4) lib/rack/session/abstract/id.rb:210:in `context' rack (1.4.4) lib/rack/session/abstract/id.rb:205:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call' activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call' activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__3609576242730521204__call__105225653498443597__callbacks' activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback' activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks' activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks' actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app' railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call' activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged' railties (3.2.11) lib/rails/rack/logger.rb:16:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call' rack (1.4.4) lib/rack/methodoverride.rb:21:in `call' rack (1.4.4) lib/rack/runtime.rb:17:in `call' activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call' rack (1.4.4) lib/rack/lock.rb:15:in `call' actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call' railties (3.2.11) lib/rails/engine.rb:479:in `call' railties (3.2.11) lib/rails/application.rb:223:in `call' rack (1.4.4) lib/rack/content_length.rb:14:in `call' railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call' rack (1.4.4) lib/rack/handler/webrick.rb:59:in `service' /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' Request Parameters: {"utf8"=>"✓", "authenticity_token"=>"/SEaqnrMf5X0pd4FVWbu8uWVAjTNw4LPKiXg+8Hl0PQ=", "session"=>{"email"=>"#", "password"=>"[FILTERED]"}, "commit"=>"Sign in"} 

我猜是的

 def has_password?(submitted_password) encrypted_password == encrypted(submitted_password) end 

应该

 def has_password?(submitted_password) encrypted_password == encrypt(submitted_password) end 

更一般地说“如何读取堆栈跟踪来解决问题”:

1)查看堆栈跟踪的顶行(文本的大墙)

 activemodel (3.2.11) lib/active_model/attribute_methods.rb:407:in `method_missing' 

这是代码中的一行吗? 在这个例子中没有:它来自activemodel。

2)继续查看,直到找到代码的第一行代码。 在这种情况下,它:

 app/models/user.rb:32:in `has_password?' 

这是导致问题的代码中的行。

3)去看导致问题的代码行。 stacktrace告诉我们这是文件app/models/user.rb第32行

在这种情况下,它是这三行的中间行:

 def has_password?(submitted_password) encrypted_password == encrypted(submitted_password) end 

4)通过阅读错误消息找出出了什么问题;)

错误消息告诉您“加密”不存在作为方法是吗?

所以请查看您的用户模型,看看您是否有一个名为encrypted的方法…

在这种情况下你没有,但你确实有一个电话“加密” – 很有可能你只是偶然输入“加密”而不是“加密”…修复它,你的问题就解决了。

这四个步骤可用于解决此类大多数错误。

好像你ruby无法识别用户对象的encrypted方法。 你应该能够通过包括解决这个问题

 require 'digest/sha1' 

为您的用户模型

例如:

 require 'digest/sha1' class User < ActiveRecord::Base #code end 

阅读此问题以了解更多关于加密gem的信息