我在Heroku上部署了Rails API(Rails API不是使用rails new <app-name> --api
创建的,而是我从中创建API的Rails应用)。
在Heroku上进行部署时,Rails API无法正常工作,并抛出内部服务器错误状态500。
我尝试使用邮递员和浏览器来访问我的API,但是我收到内部服务器错误的JSON输出。
上述JSON响应是将请求发送到my-app-name.herokuapp.com/api/v1/foods
我应该提到我曾尝试heroku run rails console
来查看我的数据库是否正确植入了种子,并且得到了所有应该正确获得的数据。
在尝试向上述网址发送请求后,我在这里复制了heroku logs --tail
的结果:
2019-06-18T19:45:53.619745+00:00 heroku[web.1]: State changed from starting to up
2019-06-18T19:45:55.010890+00:00 app[web.1]: => Booting Puma
2019-06-18T19:45:55.010918+00:00 app[web.1]: => Rails 5.2.3 application starting in production
2019-06-18T19:45:55.010920+00:00 app[web.1]: => Run `rails server -h` for more startup options
2019-06-18T19:45:55.010922+00:00 app[web.1]: Puma starting in single mode...
2019-06-18T19:45:55.010924+00:00 app[web.1]: * Version 3.12.1 (ruby 2.6.2-p47), codename: Llamas in Pajamas
2019-06-18T19:45:55.010926+00:00 app[web.1]: * Min threads: 5, max threads: 5
2019-06-18T19:45:55.010927+00:00 app[web.1]: * Environment: production
2019-06-18T19:45:55.010929+00:00 app[web.1]: * Listening on tcp://0.0.0.0:53860
2019-06-18T19:45:55.010930+00:00 app[web.1]: Use Ctrl-C to stop
2019-06-18T19:45:55.010940+00:00 app[web.1]: I, [2019-06-18T19:45:55.010760 #4] INFO -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] Started GET "/api/v1/foods" for 50.64.98.60 at 2019-06-18 19:45:55 +0000
2019-06-18T19:45:55.013576+00:00 app[web.1]: I, [2019-06-18T19:45:55.013496 #4] INFO -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] Processing by Api::V1::FoodsController#index as JSON
2019-06-18T19:45:55.099003+00:00 app[web.1]: D, [2019-06-18T19:45:55.098851 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] Food Load (2.5ms) SELECT "foods".* FROM "foods" ORDER BY "foods"."created_at" DESC
2019-06-18T19:45:55.141862+00:00 app[web.1]: D, [2019-06-18T19:45:55.141763 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] User Load (1.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 13], ["LIMIT", 1]]
2019-06-18T19:45:55.175064+00:00 app[web.1]: D, [2019-06-18T19:45:55.174961 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] User Load (1.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]]
2019-06-18T19:45:55.177292+00:00 app[web.1]: D, [2019-06-18T19:45:55.177228 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 17], ["LIMIT", 1]]
2019-06-18T19:45:55.178159+00:00 app[web.1]: D, [2019-06-18T19:45:55.178086 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]]
2019-06-18T19:45:55.179086+00:00 app[web.1]: D, [2019-06-18T19:45:55.179015 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]]
2019-06-18T19:45:55.181104+00:00 app[web.1]: D, [2019-06-18T19:45:55.181021 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] User Load (1.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 15], ["LIMIT", 1]]
2019-06-18T19:45:55.182066+00:00 app[web.1]: D, [2019-06-18T19:45:55.182006 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 15], ["LIMIT", 1]]
2019-06-18T19:45:55.183364+00:00 app[web.1]: D, [2019-06-18T19:45:55.183305 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]]
2019-06-18T19:45:55.184020+00:00 app[web.1]: D, [2019-06-18T19:45:55.183965 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 17], ["LIMIT", 1]]
2019-06-18T19:45:55.185703+00:00 app[web.1]: D, [2019-06-18T19:45:55.185634 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 17], ["LIMIT", 1]]
2019-06-18T19:45:55.186348+00:00 app[web.1]: D, [2019-06-18T19:45:55.186279 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 17], ["LIMIT", 1]]
2019-06-18T19:45:55.187878+00:00 app[web.1]: D, [2019-06-18T19:45:55.187804 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 17], ["LIMIT", 1]]
2019-06-18T19:45:55.189597+00:00 app[web.1]: D, [2019-06-18T19:45:55.189533 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 17], ["LIMIT", 1]]
2019-06-18T19:45:55.190234+00:00 app[web.1]: D, [2019-06-18T19:45:55.190175 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 13], ["LIMIT", 1]]
2019-06-18T19:45:55.191655+00:00 app[web.1]: D, [2019-06-18T19:45:55.191587 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 17], ["LIMIT", 1]]
2019-06-18T19:45:55.192344+00:00 app[web.1]: D, [2019-06-18T19:45:55.192275 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 15], ["LIMIT", 1]]
2019-06-18T19:45:55.194231+00:00 app[web.1]: D, [2019-06-18T19:45:55.194164 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 16], ["LIMIT", 1]]
2019-06-18T19:45:55.195760+00:00 app[web.1]: D, [2019-06-18T19:45:55.195683 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]]
2019-06-18T19:45:55.197667+00:00 app[web.1]: D, [2019-06-18T19:45:55.197600 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]]
2019-06-18T19:45:55.198344+00:00 app[web.1]: D, [2019-06-18T19:45:55.198264 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 15], ["LIMIT", 1]]
2019-06-18T19:45:55.208444+00:00 app[web.1]: D, [2019-06-18T19:45:55.208360 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] ActiveStorage::Attachment Load (1.5ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 [["record_id", 20], ["record_type", "Food"], ["name", "pictures"]]
2019-06-18T19:45:55.278745+00:00 heroku[router]: at=info method=GET path="/api/v1/foods" host=cookme-app-api.herokuapp.com request_id=1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c fwd="50.64.98.60" dyno=web.1 connect=0ms service=270ms status=500 bytes=257 protocol=http
2019-06-18T19:45:55.264309+00:00 app[web.1]: D, [2019-06-18T19:45:55.264204 #4] DEBUG -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] ActiveStorage::Blob Load (1.5ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 [["id", 37]]
2019-06-18T19:45:55.273452+00:00 app[web.1]: I, [2019-06-18T19:45:55.273361 #4] INFO -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] [active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::Attributes (147.01ms)
2019-06-18T19:45:55.273676+00:00 app[web.1]: I, [2019-06-18T19:45:55.273619 #4] INFO -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] Completed 500 Internal Server Error in 260ms (ActiveRecord: 53.5ms)
2019-06-18T19:45:55.274332+00:00 app[web.1]: F, [2019-06-18T19:45:55.274278 #4] FATAL -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c]
2019-06-18T19:45:55.274379+00:00 app[web.1]: F, [2019-06-18T19:45:55.274335 #4] FATAL -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):
2019-06-18T19:45:55.274423+00:00 app[web.1]: F, [2019-06-18T19:45:55.274381 #4] FATAL -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c]
2019-06-18T19:45:55.274474+00:00 app[web.1]: F, [2019-06-18T19:45:55.274433 #4] FATAL -- : [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] app/serializers/food_collection_serializer.rb:31:in `block in pictures'
2019-06-18T19:45:55.274476+00:00 app[web.1]: [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] app/serializers/food_collection_serializer.rb:25:in `map'
2019-06-18T19:45:55.274477+00:00 app[web.1]: [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] app/serializers/food_collection_serializer.rb:25:in `pictures'
2019-06-18T19:45:55.274479+00:00 app[web.1]: [1bcb1fe7-acf5-44e0-95bc-aa20d5fd685c] app/controllers/api/v1/foods_controller.rb:12:in `index'
答案 0 :(得分:2)
错误提示:
ArgumentError(缺少要链接的主机!请提供:host参数,将default_url_options [:host]或将:only_path设置为true):
您需要在生产配置中添加默认主机:
config / environments / production.rb
Rails.application.routes.default_url_options = { host: "your heroku domain" }
希望有帮助。