【问题标题】:'Rake db:seed' command returning http error 500 in heroku'Rake db:seed' 命令在 heroku 中返回 http 错误 500
【发布时间】:2019-10-05 08:02:55
【问题描述】:

我在 Heroku 上的 Rails 中部署 api 时遇到问题。在开发环境中它工作得很好,我通过 db:seed 放置的数据在数据库中。在 heroku 上进行部署后,我运行了 db:migrate 命令,我的银行是空的,当运行 db:seed 时,服务器返回 http 500 错误。我做错了什么吗?有没有可能在heroku中使用db:seed?

Product.destroy_all
   ProductCategory.destroy_all
   Restaurant.destroy_all
   Category.destroy_all


   path_image = 'public/images/categories/mexican.jpg'
   c = Category.create(id: 1, title: 'mexican')
   c.image.attach(io: File.open(path_image), filename: 'mexican.jpg')

   path_image = 'public/images/categories/italian.jpeg'
   c = Category.create(id: 2, title: 'italian')
   c.image.attach(io: File.open(path_image), filename: 'italian.jpeg')

   path_image = 'public/images/categories/japonese.jpeg'
   c = Category.create(id: 3, title: 'japanese')
   c.image.attach(io: File.open(path_image), filename: 'japanese.jpeg')

   path_image = 'public/images/categories/vegan.jpeg'
   c = Category.create(id: 4, title: 'vegan')
   c.image.attach(io: File.open(path_image), filename: 'vegan.jpeg')


【问题讨论】:

  • “运行 db:seed 时,服务器返回 http 500 错误”是什么意思? HTTP 响应代码来自 HTTP 请求,但 db:seed 不发出 HTTP 请求。
  • 感谢您的回复。当我运行“heroku run db: migrate”时,它会迁移我的数据库和所有表,但数据是空的。运行“heroku run rails db:seed”后,在heroku中打开应用程序时,浏览器返回错误500。你认为我应该在seeds.rb中进行一些调整吗?我把我的seeds.rb 代码放在了编辑中
  • 哦。所以heroku run rails db:seed“工作”,在它成功完成的意义上。 HTTP 500 是一个通用的服务器端错误消息。您的日志中有什么(尝试运行 heroku logs)?

标签: ruby-on-rails postgresql heroku rake heroku-postgres


【解决方案1】:

试试,heroku 运行 rails db:seed

【讨论】:

  • 感谢您的回复。我将命令与 heroku run 一起使用。使用“heroku run db:migrate”它会迁移我的数据库和所有表,但数据是空的。执行“heroku run rails db:seed”后,heroku中的应用程序返回错误500。这个错误不是由于使用本地图像造成的吗?我把我的seeds.rb 代码放在了编辑中
  • 在查看了您的种子之后,您如何实现图像我强烈建议您使用图像服务器。我最流行的一个,免费,易于实现 Rails 是云的,如果有必要在客户端上传图片也可以使用 carriwave gem ...cloudinary.com/documentation/rails_integration#installation
【解决方案2】:

首先确保你做到了heroku rails db:migrate 然后做heroku rails db:seed

如果您直接在 heroku 终端上编写,则不需要“he​​roku”,只需 rails db:migraterails db:seed

【讨论】:

  • 感谢您的回复。我将命令与 heroku run 一起使用。使用“heroku run db:migrate”它会迁移我的数据库和所有表,但数据是空的。执行“heroku run rails db:seed”后,heroku中的应用程序返回错误500。这个错误不是由于使用本地图像造成的吗?我把我的seeds.rb 代码放在了编辑中
  • 我不认为这可能是由于本地图像,如果它的本地图像不会给出错误 500,请转到 /configs/environments/production.rb 然后更改 config.consider_all_requests_local = false 并更改将它发送到true 这样,您将看到您的错误并知道它为什么显示该错误代码。完成后将其更改回 false,这样用户就不会在生产中看到错误代码。如果对您有用,请对此答案投票并标记为正确。
猜你喜欢
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
相关资源
最近更新 更多