Heroku 运行 rake db:seed 给出预期 keyword_end

Heroku run rake db:seed gives expecting keyword_end

我正在学习 hartle 教程的第 11 章。 本章结束我运行

heroku run rake db:seed

我得到了这个错误:

rake aborted!
SyntaxError: /app/db/seeds.rb:23: syntax error, unexpected end-of-input, expecting keyword_end

这是我的 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
users = User.order(:created_at).take(6)
50.times do
  content = Faker::Lorem.sentence(5)
  users.each { |user| user.microposts.create!(content: content) }
end

到目前为止,我对 seed.db 没有任何问题!

"expected keyword_end" 错误表示您在指定文件中有语法错误。您会经常看到这一点,尤其是在您的测试套件中;适应它。

用细齿梳理文件,并在脑海中将每个 { 与其 } 和每个 do 与其 end 匹配。通常你会发现其中一个不匹配。

如果找不到不匹配的地方,请注释掉整个种子文件并再次 运行 rake db:seed。该错误不应出现。然后逐步取消注释文件的每个部分,直到找出错误的来源。

这很乏味,但很管用。

我遇到了类似的问题

class 标签 < ApplicationRecord::Base has_many:目的地 结尾 结束

我的模型中有双 "end end "...我删除了一个 "end",问题得到解决

只需 运行 您在终端中的文件,您应该会获得足够的信息来查找旧的 unexpected end-of-input-语法错误。如果您指定 -w 选项,您还会看到额外的警告,通常包含您需要的相关提示。

在你的情况下:运行 在你的终端中(从你的项目的根目录):

ruby -w db/seeds.rb

现在您应该得到一个输出,详细说明 seeds.rb 中的语法错误和行号(以及其他警告和错误,如果有的话),例如类似于:

db/seeds.rb:19: warning: mismatched indentations at 'end' with 'unless' at 12
db/seeds.rb:25: syntax error, unexpected end-of-input, expecting keyword_end
...
...