How to set config.action_controller.default_url_options = {:host = '#''} on per environment basis

Ruby on-RailsRuby on-Rails-3Actioncontroller

Ruby on-Rails Problem Overview


Right now I'm using this which works for the development host, but I have to manually change the {:host => ""} code when I move to production.

post.rb
def share_all
  url =  Rails.application.routes.url_helpers.post_url(self, :host => 'localhost:3000')
  if user.authentications.where(:provider => 'twitter').any?
    user.twitter_share(url)  
  end
end

I'd like to use this and then define the default_url_options per environment:

post.rb
def share_all
  url =  Rails.application.routes.url_helpers.post_url(self)
  if user.authentications.where(:provider => 'twitter').any?
    user.twitter_share(url)  
  end
end

I've tried adding this to my config/environments/development.rb but I still get the "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" error

development.rb
config.action_controller.default_url_options = {:host => "localhost:3000"}

And I even tried it this way:

development.rb
config.action_controller.default_url_options = {:host => "localhost", :port => "3000"}

EDIT:

I've now also followed this and still the same error guide http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options

application controller
class ApplicationController < ActionController::Base
  protect_from_forgery
  include ApplicationHelper
  def default_url_options
    if Rails.env.production?
      { :host => "example.com"}
    else
      {:host => "example1.com"}
    end
  end
end

This is driving me crazy, what am I missing here???

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Okay I figured it out the correct way to write it is

Rails.application.routes.default_url_options[:host] = 'localhost:3000'

:)

Solution 2 - Ruby on-Rails

Inherit your Application's default_url_options from ActionMailer.

You want to keep things as DRY as possible so, ideally, you don't want to hard code your host and port in multiple places for the same environment, unless your ActionMailer actually uses a different host and port than the rest of your Application.

To set the default_url_options for your entire Application, simply add the following line to your config/environment.rb file (changing MyApp to your app's name):

# Set the default host and port to be the same as Action Mailer.
MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options

This will fix your problem and automatically set your Application's default_url_options to the same as your config.action_mailer.default_url_options:

$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}

$ MyApp::Application.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}

Solution 3 - Ruby on-Rails

You have to restart your server before the changes to this file takes effect.

Solution 4 - Ruby on-Rails

config.action_mailer.default_url_options = { :host => "your host" }

for instance your host localhost:3000

you can put this in test.rb, development.rb, production.rb files host could be different from environment to environment

Solution 5 - Ruby on-Rails

config/environments/development.rb (any other environment, same)

add this row with host that you want

routes.default_url_options[:host] = 'localhost:3000'

Solution 6 - Ruby on-Rails

I know this is an old thread, but I ran into this with Ruby 2.6.3 and Rails 5.2.3. The behavior I was seeing was basically that every path I added would fail with Error during failsafe response: undefined method 'empty?' for nil:NilClass. In production it worked fine, but in my development environment, I would get the error mentioned above.


The fix for me was add this to controllers/application_controller.rb:

def default_url_options
  if Rails.env.production?
    Rails.application.routes.default_url_options = { host: "www.production-domain.com", protocol: 'https' }
  elsif Rails.env.development?
    Rails.application.routes.default_url_options = { host: 'localhost:3000', protocol: 'http' }
  end
end

I was then able to run my development environment on my local.

Solution 7 - Ruby on-Rails

Just ran into this problem myself trying to generate a URL in a rake task. It's definitely non-obvious and many of these solutions will work. My solution is a take off from @joshua-pinter's but done in the environment file. For example my development.rb looks like:

Rails.application.configure doconfig.action_mailer.default_url_options = self.default_url_options = { host: 'localhost', port: 3000 }
  …
end

with the appropriate changes made to production.rb.

Solution 8 - Ruby on-Rails

For me whats worked is

ActionMailer::Base.default_url_options = { host: "yourhosthere"} # e.g. yourhosthere=localhost:3000 or yourhosthere=example.com

because if you already set a port in the config files then changing the [:host] only will result in an error

TypeError (no implicit conversion of Symbol into Integer):

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontrying_hal9000View Question on Stackoverflow
Solution 1 - Ruby on-Railstrying_hal9000View Answer on Stackoverflow
Solution 2 - Ruby on-RailsJoshua PinterView Answer on Stackoverflow
Solution 3 - Ruby on-RailsRyan BiggView Answer on Stackoverflow
Solution 4 - Ruby on-RailsAshok Reddy ReddemView Answer on Stackoverflow
Solution 5 - Ruby on-RailsИгорь ХлебниковView Answer on Stackoverflow
Solution 6 - Ruby on-RailsSean GriffinView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAaronView Answer on Stackoverflow
Solution 8 - Ruby on-RailsdowiView Answer on Stackoverflow