0

我正在阅读 Michael Hartl 的教程,但没有收到错误消息:

undefined local variable or method `n' for main:Object

当我跑步时bundle exec rake db:populate

sample_data.rake 文件

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    name  = Faker::Name.name
    email = "example-#{n+1}@railstutorial.org"
    password  = "password"
    User.create!(name: name,
                   email: email,
                   password: password,
                   password_confirmation: password)
  end
end
4

2 回答 2

1

您在电子邮件旁边有 n 个内插字符串,它没有在任何地方定义。删除或定义它。

于 2013-08-09T22:05:56.897 回答
1

只需在脚本开头将n变量初始化为.0name

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    n = 0
    name  = Faker::Name.name
    email = "example-#{n+1}@railstutorial.org"
    password  = "password"
    User.create!(name: name,
                   email: email,
                   password: password,
                   password_confirmation: password)
  end
end

这段代码很可能是循环使用的。在这里,您实际上不需要n.

于 2013-08-09T22:12:59.533 回答