Ruby on Rails:未定义的方法 'digest' Rails 教程第 10.1.1 节
Ruby on Rails: undefined method 'digest' Rails Tutorial Section 10.1.1
我目前正在学习 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
谁能帮我解决这个错误?在过去的两周里,这让我很沮丧。谢谢。
您尚未定义 class 方法 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
看来你可能错过了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
我目前正在学习 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
谁能帮我解决这个错误?在过去的两周里,这让我很沮丧。谢谢。
您尚未定义 class 方法 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
看来你可能错过了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