NoMethodError 的完整跟踪 [英] Full Trace for NoMethodError

查看:40
本文介绍了NoMethodError 的完整跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is essentially part 2. of my prior question, as requested I have created a new question due to the length of the code. Hope we can solve this, with your help I am sure that we can find a solution now with the new information.

感谢您的输入.这是pt的链接.1 使用实际代码:登录功能不起作用.

Thanks for the input. here is the link to pt. 1 with the actual code: Sign in function not working.

ps.请不要忘记为这个问题投票.

ps. Pls don't forget to vote this question up.

这里是 user.rb:

Heres the 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 < ActiveRecord::Base
  attr_accessor   :password
  attr_accessible :email, :name, :password, :password_confirmation

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, :presence => 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 #<User:0x007fdcde331a90>

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

<小时>

更一般的如何阅读堆栈跟踪来解决问题":


More generally "how to read a stacktrace to solve a problem":

1) 查看 stacktrace 的顶行(大墙)

1) look at the top line of the stacktrace (the big wall of text)

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

这是您的代码中的一行吗?在这种情况下不是:它来自 activemodel.

Is that a line in your code? in this instance no: it's from activemodel.

2) 继续查找,直到找到用于您的 代码的第一行代码.在这种情况下:

2) keep looking until you get to the first line of code that is for your code. In this case its:

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

这是导致问题的您的代码行.

This is the line in your code that is causing the problem.

3) 查看导致问题的代码行.堆栈跟踪告诉我们这是文件 app/models/user.rb

3) go look at the line of code that's causing the problem. The stacktrace tell us that this is line 32 of the file app/models/user.rb

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

In this case it's the middle line of these three lines:

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

4) 通过阅读错误消息找出问题所在;)

4) figure out what went wrong, by reading the error message ;)

错误消息告诉您加密"作为一种方法不存在是吗?

the error message is telling you that "encrypted" does not exist as a method yes?

所以看看你的用户模型,看看你是否有一个叫做加密的方法......

So look in your user model and see if you have a method called encrypted...

在这种情况下,您没有,但您确实有一个调用加密"-很有可能您只是不小心输入了加密"而不是加密"......解决这个问题,您的问题就解决了.

In this case you don't, but you do have one call "encrypt" - good chance that you've just accidentally typed "encrypted" instead of "encrypt"... fix that and you problem is solved.

这四个步骤可以解决大多数此类错误.

These four steps can be used to solve most errors of this kind.

这篇关于NoMethodError 的完整跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆