Ruby on Rails:未定义的方法 'digest' Rails 教程第 10.1.1 节
Posted
技术标签:
【中文标题】Ruby on Rails:未定义的方法 \'digest\' Rails 教程第 10.1.1 节【英文标题】:Ruby on Rails: undefined method 'digest' Rails Tutorial Section 10.1.1Ruby on Rails:未定义的方法 'digest' Rails 教程第 10.1.1 节 【发布时间】:2015-11-13 03:44:00 【问题描述】:我目前正在关注 Michael Hartl 的 Rails 教程,但在 Section 10.1.1 上遇到问题,我应该在我的用户模型中创建一个 create_activation_digest
操作,该操作会创建并分配一个激活令牌并在注册时向每个用户摘要。
我首先使用以下方法重置我的迁移:
bundle exec rake db:migrate:reset
然后我使用以下方法将示例用户播种到我的数据库中:
bundle exec rake db:seed
但我收到一条错误消息:
NoMethodError: undefined method `digest' for #<Class:0x007fb23ae0dcd0>
我在下面提供了完整的错误消息。
这是我的user.rb
文件的样子:
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token
before_save :downcase_email
before_create :create_activation_digest
# Name Validations
validates :name, presence: true, length: maximum: 50
# Email Validations
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: maximum: 255 ,
format: with: VALID_EMAIL_REGEX ,
uniqueness: case_sensitive: false
# Password Validations
has_secure_password
validates :password, presence: true, length: minimum: 6
# Returns a random token
def User.new_token
SecureRandom.urlsafe_base64
end
# Remembers a user in the database for use in persistent sessions
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
# Returns true if the given token matches the digest
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
# Forgets a user
def forget
update_attribute(:remember_digest, nil)
end
private
# Converts email to all lower-case
def downcase_email
self.email = email.downcase
end
# Creates and assigns the activation token and digest
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
end
这是我在终端收到的错误消息:
0587343072:ruby_rails yuriramocan$ bundle exec rake db:seed --trace
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
rake aborted!
NoMethodError: undefined method `digest' for #<Class:0x007fb23ae0dcd0>
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
/Users/yuriramocan/Development/ruby_rails/app/models/user.rb:50:in `create_activation_digest'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:430:in `block in make_lambda'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:162:in `call'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:162:in `block in halting'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:502:in `call'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:502:in `block in call'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:502:in `each'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:502:in `call'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:88:in `run_callbacks'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/callbacks.rb:306:in `_create_record'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/timestamp.rb:57:in `_create_record'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/persistence.rb:504:in `create_or_update'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/callbacks.rb:302:in `block in create_or_update'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:115:in `call'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:115:in `call'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:553:in `block (2 levels) in compile'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:503:in `call'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:503:in `call'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:88:in `run_callbacks'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/callbacks.rb:302:in `create_or_update'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/persistence.rb:142:in `save!'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/validations.rb:43:in `save!'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/transactions.rb:291:in `block in save!'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/transactions.rb:220:in `transaction'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/transactions.rb:291:in `save!'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/persistence.rb:51:in `create!'
/Users/yuriramocan/Development/ruby_rails/db/seeds.rb:1:in `<top (required)>'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `block in load'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/railties-4.2.3/lib/rails/engine.rb:547:in `load_seed'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Users/yuriramocan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/yuriramocan/.rvm/rubies/ruby-2.2.2/bin/rake:33:in `<main>'
Tasks: TOP => db:seed
另外,这是我的seeds.rb
文件,以供参考:
User.create!(name: "Example User",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true,
activated: true,
activated_at: Time.zone.now)
99.times do |n|
name = Faker::Name.name
email = "example-#n+1@railstutorial.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password,
activated: true,
activated_at: Time.zone.now)
end
谁能帮我解决这个错误?在过去的两周里,我一直很沮丧。谢谢。
【问题讨论】:
【参考方案1】:你还没有定义类方法digest
,它应该看起来像
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
代码复制自 Michael Hartl https://www.railstutorial.org/book/log_in_log_out
【讨论】:
哇,是的!就是这样。我完全错过了。太感谢了。 @Rahul【参考方案2】:看来你可能错过了listing 8.18:
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
【讨论】:
以上是关于Ruby on Rails:未定义的方法 'digest' Rails 教程第 10.1.1 节的主要内容,如果未能解决你的问题,请参考以下文章
Ruby on Rails:未定义的方法,检查boolean if true语句
Base64 编码的字符串到文件(Ruby on Rails) - 未定义的方法“解包”错误
Ruby on Rails Simple_form_for的未定义方法
Ruby on Rails - 未定义的方法`split'代表nil:StocksController中的NilClass / NoMethodError #search
nameerror 未定义的局部变量或方法 `log_out' 你的意思是? logout_url ruby on rails