无法在rails 4.x中输入数据库中的数据

时间:2014-05-28 01:00:36

标签: ruby-on-rails-4 railstutorial.org

我在Rails教程第9章的代码清单9.29中尝试执行以下rake任务:

namespace :db do
 desc "Fill database with sample data"
 task populate: :environment do
 User.create!(name: "Example user", email: "example@railstutorial.org", password: "foobar", password_confirmation: "foobar")
  99.times do |n|
    Rails.logger.debug "populate: n= "+n.to_s
    name = Faker::Name.name
    Rails.logger.debug "populate: name= "+name.to_s
    email = "example-#{n+1}@railstutorial.org"
    Rails.logger.debug "populate: email= "+email.to_s
    password = "password"
    User.create!(name: name, password: password, password_confirmation: password)
  end
 end
end

我正在尝试按如下方式运行任务:

$ bundle exec rake db:reset
$ bundle exec rake db:populate

但是我收到了一个错误:

sb7904313:sample_app nnikolo$ bundle exec rake db:populate --trace
** Invoke db:populate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:populate
D, [2014-05-28T01:37:49.630091 #58879] DEBUG -- :    (0.1ms)  begin transaction
D, [2014-05-28T01:37:49.637857 #58879] DEBUG -- :   User Exists (0.2ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('example@railstutorial.org') LIMIT 1
E, [2014-05-28T01:37:49.641162 #58879] ERROR -- : Binary data inserted for `string` type on column  `password_digest`
D, [2014-05-28T01:37:49.641661 #58879] DEBUG -- :   SQL (2.5ms)  INSERT INTO "users" ("created_at", "email", "name", "password_digest", "remember_token", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Wed, 28 May 2014 00:37:49 UTC +00:00], ["email", "example@railstutorial.org"], ["name", "Example user"], ["password_digest", "$2a$10$8wKIeQe/Zzjy64kgnfcKcuudLP2PVisXNYxPkomHbi9kXeVQLzgdu"], ["remember_token", "9b55166615f92aa5145d3c49aae036f3b460f46a"], ["updated_at", Wed, 28 May 2014 00:37:49 UTC +00:00]]
D, [2014-05-28T01:37:49.643905 #58879] DEBUG -- :    (2.0ms)  commit transaction
D, [2014-05-28T01:37:49.644121 #58879] DEBUG -- : populate: n= 0
D, [2014-05-28T01:37:49.782449 #58879] DEBUG -- : populate: name= Demetrius Walter
D, [2014-05-28T01:37:49.782531 #58879] DEBUG -- : populate: email= example-1@railstutorial.org
D, [2014-05-28T01:37:49.841975 #58879] DEBUG -- :    (0.1ms)  begin transaction
D, [2014-05-28T01:37:49.844404 #58879] DEBUG -- :   User Exists (0.2ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
D, [2014-05-28T01:37:49.845226 #58879] DEBUG -- :    (0.1ms)  rollback transaction rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Email can't be blank, Email is invalid
/Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activerecord-4.0.4/lib/active_record/validations.rb:57:in `save!'

为什么我收到此消息“电子邮件是空白的”? rake任务明确为电子邮件设置了一个值?

1 个答案:

答案 0 :(得分:2)

您没有通过email

User.create!(name: name, password: password, password_confirmation: password)

添加email参数:

User.create!(email: email, name: name, password: password, password_confirmation: password)
相关问题