无法在Heroku上部署Rails Dynamic路由器

时间:2018-07-27 12:02:14

标签: ruby-on-rails heroku

我正在基于模型属性进行重定向。 它在本地工作正常,但是当我按下然后完成部署时,Heroku会向我发送调试错误。

下面的代码有什么问题? (来自here

2018-07-27T11:18:21.949383+00:00 app[web.1]: D, [2018-07-27T11:18:21.076083 #4] DEBUG -- :   Passenger Load (1.3ms)  SELECT "passengers".* FROM "passengers"
2018-07-27T11:18:21.949385+00:00 app[web.1]: Puma starting in single mode...
2018-07-27T11:18:21.949384+00:00 app[web.1]: D, [2018-07-27T11:18:21.893911 #4] DEBUG -- :   Passenger Load (1.6ms)  SELECT "passengers".* FROM "passengers"
2018-07-27T11:18:21.949393+00:00 app[web.1]: * Version 3.12.0 (ruby 2.4.1-p111), codename: Llamas in Pajamas
2018-07-27T11:18:21.949396+00:00 app[web.1]: * Environment: production
2018-07-27T11:18:21.949395+00:00 app[web.1]: * Min threads: 5, max threads: 5
2018-07-27T11:18:21.949532+00:00 app[web.1]: * Listening on tcp://0.0.0.0:50520
2018-07-27T11:18:21.950026+00:00 app[web.1]: Use Ctrl-C to stop
2018-07-27T11:18:22.440272+00:00 heroku[web.1]: State changed from starting to up
2018-07-27T11:21:16.609823+00:00 heroku[web.1]: Restarting
2018-07-27T11:21:16.610597+00:00 heroku[web.1]: State changed from up to starting
2018-07-27T11:21:17.485929+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2018-07-27T11:21:17.501136+00:00 app[web.1]: - Gracefully stopping, waiting for requests to finish
2018-07-27T11:21:17.517026+00:00 app[web.1]: Exiting
2018-07-27T11:21:17.517896+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:430:in `close': stream closed (IOError)
2018-07-27T11:21:17.517967+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:433:in `handle_servers'
2018-07-27T11:21:17.517939+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:430:in `ensure in handle_servers'
2018-07-27T11:21:17.517991+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.4.0/gems/puma-3.12.0/lib/puma/server.rb:358:in `block in run'
2018-07-27T11:21:17.608339+00:00 heroku[web.1]: Process exited with status 1

如果我尝试执行db:migrate,则会引发

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "passengers" does not exist
LINE 1: SELECT "passengers".* FROM "passengers" 

app/models/dynamic_router.rb 其中包含有问题的应该本来可以工作的loop passenger.all.each do..end吗?

class DynamicRouter
  def self.load
    Rails.application.routes.draw do
      # faulty part
      Passenger.all.each do |pg|
        get "/#{pg.shortcut}" => redirect("/users/sign_up?q=#{pg.id}&t=#{pg.token}")
      end
        #end of faulty part
    end
  end

  def self.reload
    Rails.application.routes_reloader.reload!
  end
end

app/models/passenger.rb

  after_save :reload_routes

  def reload_routes
  DynamicRouter.reload
  end

config/routes.rb

Rails.application.routes.draw do
...    
DynamicRouter.load

end

1 个答案:

答案 0 :(得分:0)

这是因为Heroku不接受来自route.config的数据库调用 找到了解决方法here

get ":shortcut" => redirect("/users/sign_up"), constraints: lambda { |request| Passenger.where(shortcut: request[:shortcut]).any? }