You need to pass the host as an argument in the call in which you use the helper:
Rails.application.routes.url_helpers.media_url(:host => "localhost:3000")
or another route like this:
Rails.application.routes.url_helpers.media_url(self, :host => AppConfig.host)
where AppConfig.host is the host depending on the environment (localhost: 300 for env development).
source share