Search code examples
ruby-on-railsherokurails-api

Deployed Rails API on Heroku throws internal server error (500) while it works on development


I deployed my Rails API on Heroku (Rails API was not created using rails new <app-name> --api, but instead it was a Rails app that I created an API from it).

When deploying on Heroku, Rails API doesn't work and throws Internal Server Error status 500.

I have tried to reach out to my API using postman and also using the browser, but I am getting a JSON output of internal server error.

The above-mentioned JSON response is the result of sending requests to my-app-name.herokuapp.com/api/v1/foods

I should mention that I tried heroku run rails console to see if my database was seeded correctly and I got all the data that I was supposed to get correctly.

Here I copied the results of the heroku logs --tail after trying to send a request to the above mentioned url:

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'


Solution

  • As the error suggests:

    ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):

    You need to add the default host in your production configuration:

    config/environments/production.rb

    Rails.application.routes.default_url_options = { host: "your heroku domain" }