我正在关注 Ruby on Rails 指南,但遇到了使用 gem“Faker”生成虚假内容的问题。我安装了 faker 并按照说明将用户和照片填充到我的项目中。我在 lib/tasks/populate.rake 中创建了这个文件
lib/tasks/populate.rake
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
10.times do |n|
puts "[DEBUG] creating user #{n+1} of 10"
name = Faker::Name.name
email = "user-#{n+1}@example.com"
password = "password"
User.create!( name: name,
email: email,
password: password,
password_confirmation: password)
end
User.all.each do |user|
puts "[DEBUG] uploading images for user #{user.id} of #{User.last.id}"
10.times do |n|
image = File.open(Dir.glob(File.join(Rails.root, 'sampleimages', '*')).sample)
description = %w(cool awesome crazy wow adorbs incredible).sample
user.pins.create!(image: image, description: description)
end
end
end
end
现在我要做的就是将“rake db:populate”放在我的终端(以及正确的文件夹中)。当我这样做时,我得到:
rake db:populate
[DEBUG] creating user 1 of 10
[DEBUG] creating user 2 of 10
[DEBUG] creating user 3 of 10
[DEBUG] creating user 4 of 10
[DEBUG] creating user 5 of 10
[DEBUG] creating user 6 of 10
[DEBUG] creating user 7 of 10
[DEBUG] creating user 8 of 10
[DEBUG] creating user 9 of 10
[DEBUG] creating user 10 of 10
[DEBUG] uploading images for user 15 of 25
rake aborted!
can't convert nil into String
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:18:in `initialize'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:18:in `open'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:18:in `block (4 levels) in <top (required)>'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:17:in `times'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:17:in `block (3 levels) in <top (required)>'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:15:in `each'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:15:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
Tasks: TOP => db:populate
(See full trace by running task with --trace)
我最关心的是
“rake 中止!无法将 nil 转换为字符串”
它似乎创建了用户,但没有上传任何图片。
问题:
a) 为什么 rake 被中止?
b) 如何删除我创建的用户?我知道的唯一方法是“User.all”,然后是 User.delete(在此处输入一个数字)......有更有效的方法吗?
帮助将不胜感激:) 谢谢!