3

Locally, it works. In Heroku production, I keep getting this error message when using ActiveModelSerializers. I've using active_model_serializers (~> 0.10.2)

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

I've read through

I've tried defining it in ./app/config/environments/production.rb

Rails.application.configure do

  config.action_mailer.default_url_options = { host: 'lit-brushlands- 
 65490.herokuapp.com' }

end 

I've tried defining it in ./app/serializers/application_serializer.rb like I see Drifting Ruby do it at 2:30 into the video.

# ./app/serializers/application_serializer.rb
class ApplicationSerializer < ActiveModel::Serializer

  include Rails.application.routes.url_helpers
  default_url_options[:host] = 'lit-brushlands-65490.herokuapp.com'

end

I've also tried defining it in ./config/application.rb like how Remear suggested to do it as the last comment on this pull request of ActiveModelSerializers.

Here's my stack trace. I try fetching a users profile photo in a decorator.

enter image description here

2018-05-29T12:40:31.274554+00:00 app[web.1]: I, [2018-05-29T12:40:31.274447 #4]  INFO -- : [b70376bf-4950-4376-bf91-f21a3533e9d3] [active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::Attributes (1766.98ms)
2018-05-29T12:40:31.274941+00:00 app[web.1]: I, [2018-05-29T12:40:31.274854 #4]  INFO -- : [b70376bf-4950-4376-bf91-f21a3533e9d3] Completed 500 Internal Server Error in 1877ms (ActiveRecord: 126.0ms)
2018-05-29T12:40:31.276753+00:00 app[web.1]: F, [2018-05-29T12:40:31.276654 #4] FATAL -- : [b70376bf-4950-4376-bf91-f21a3533e9d3]
2018-05-29T12:40:31.276834+00:00 app[web.1]: F, [2018-05-29T12:40:31.276754 #4] FATAL -- : [b70376bf-4950-4376-bf91-f21a3533e9d3] ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):
2018-05-29T12:40:31.276923+00:00 app[web.1]: F, [2018-05-29T12:40:31.276835 #4] FATAL -- : [b70376bf-4950-4376-bf91-f21a3533e9d3]
2018-05-29T12:40:31.277014+00:00 app[web.1]: F, [2018-05-29T12:40:31.276930 #4] FATAL -- : [b70376bf-4950-4376-bf91-f21a3533e9d3] app/decorators/user_decorator.rb:13:in `most_recent_profile_photo'
2018-05-29T12:40:31.277017+00:00 app[web.1]: [b70376bf-4950-4376-bf91-f21a3533e9d3] app/serializers/user_serializer.rb:11:in `profile_photo_url'
2018-05-29T12:40:31.277019+00:00 app[web.1]: [b70376bf-4950-4376-bf91-f21a3533e9d3] app/controllers/v1/posts_controller.rb:10:in `index
James Z
  • 12,209
  • 10
  • 24
  • 44
PrimeTimeTran
  • 1,807
  • 2
  • 17
  • 29

2 Answers2

1

I assume you are using carrierwave with AWS. It's possible the error is not coming directly from Serializer, check your setttings for carrierwave

Maybe you are using different host in development and that needs change

CarrierWave.configure do |config|
  config.fog_provider = 'fog/aws'                        # required
  config.fog_credentials = {
    provider:              'AWS',                        # required
    aws_access_key_id:     'xxx',                        # required
    aws_secret_access_key: 'yyy',                        # required
    region:                'eu-west-1',                  # optional, defaults to 'us-east-1'
    host:                  's3.example.com',             # optional, defaults to nil
    endpoint:              'https://s3.example.com:8080' # optional, defaults to nil
  }
  config.fog_directory  = 'name_of_directory'                                   # required
  config.fog_public     = false                                                 # optional, defaults to true
  config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" } # optional, defaults to {}
end
Przemek Mroczek
  • 357
  • 1
  • 11
1

I am not using ActiveModelSerializers but I had the same error but this did not solve it for me:

include Rails.application.routes.url_helpers

I fixed it by updating my production environment file as follows:

# production.rb

Rails.application.routes.default_url_options[:host] ='lit-brushlands-65490.herokuapp.com'

rmcsharry
  • 5,363
  • 6
  • 65
  • 108
Sergii Chub
  • 107
  • 1
  • 3
  • 3
    When you answer a technical question, it is important to provide narrative that explains why and how you chose your answer. For instance, how does your answer differ from what the original poster had in their question? – Bob Dalgleish Dec 03 '18 at 15:04
  • @BobDalgleish I choose that answer because I resolve same problem in my case. And the difference what I'm not using a `ActiveModel::Serializer`, so `include Rails.application.routes.url_helpers` doesn't work properly in my opinion. Does this answer satisfy you? – Sergii Chub Dec 04 '18 at 10:12