【问题标题】:NoMethodError: undefined method `digest'NoMethodError:未定义的方法“摘要”
【发布时间】:2016-09-02 15:40:52
【问题描述】:

我正在学习 Rails 教程,我正在尝试 test a valid user login. 但是,当我运行测试时,我得到了 NoMethodError: undefined method `digest'。我已经查看了所有内容,但不明白为什么会出现此错误。我什至求助于复制/粘贴教程代码,以确保它不是拼写错误或我在输入时遗漏的其他内容。

这是我的用户模型:

class User < ApplicationRecord
  before_save { email.downcase! }
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }

  def User.digest(string)
      cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                    BCrypt::Engine.cost
      BCrypt::Password.create(string, cost: cost)
  end
end

还有我的固定装置:

test_user:
    name: Test User
    email: testuser@example.com
    password_digest: <%= User.digest('password') %>

和我的测试:

test "login with valid information" do
  get login_path
  post login_path, params: { session: { email:    @user.email,
                                        password: 'password' } }
  assert_redirected_to @user
  follow_redirect!
  assert_template 'users/show'
  assert_select "a[href=?]", login_path, count: 0
  assert_select "a[href=?]", logout_path
  assert_select "a[href=?]", user_path(@user)
end

我很困惑,所以任何建议都值得赞赏。

堆栈跟踪:

NoMethodError: undefined method `digest' for #<User:0x000000069b8c18>
Did you mean?  Digest
  from /usr/local/rvm/gems/ruby-2.3.0/gems/activemodel-5.0.0.1/lib/active_model/attribute_methods.rb:433:in `method_missing'
  from (irb):8
  from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
  from /home/ubuntu/workspace/bin/rails:9:in `<top (required)>'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `block in load'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `call'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in `call'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:191:in `block in serve'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
  from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
  from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
  from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

【问题讨论】:

  • 是否可以在 Rails 控制台中访问摘要方法
  • @rajeevmaash 不。尝试在控制台中调用该方法时,我遇到与以前相同的错误。
  • 你能添加堆栈跟踪吗
  • @rajeevmaash 添加了
  • 您在用户类的实例上调用它。不上课。这就是错误的原因。

标签: ruby-on-rails ruby


【解决方案1】:

我发现它不喜欢函数名'digest'。当我在 User.rb 文件和 users.yaml 文件中将函数名称更改为“digestx”时,测试通过了

【讨论】:

    猜你喜欢
    • 2013-04-24
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 2019-06-12
    • 2013-11-14
    • 2021-07-07
    相关资源
    最近更新 更多