我正在关注Ruby on Rails指南并遇到使用gem“Faker”生成虚假内容的问题。我安装了faker并按照说明将用户和照片填充到我的项目中。我在lib / tasks / populate.rake
中创建了这个文件LIB /任务/ 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)
我最关心的是
“耙子流产了! 无法将nil转换为String“它似乎创建了用户,但没有上传任何图片。
问题:
a)为什么耙子被中止?
b)如何删除我创建的用户?我知道的唯一方法是“User.all”然后是User.delete(在这里放一个数字)......有更有效的方法吗?
非常感谢帮助:)谢谢!
答案 0 :(得分:0)
您的<rails_root>/sampleimages
目录为空。 Dir.glob(...)
正在返回一个空数组,当您致电sample
时,它会返回nil
。当你致电File.open(nil)
时,你会得到例外并耙中止。
如果要删除users表中的所有行,可以执行User.destroy_all
。