73

I would like to do something like this

config.default_host = 'www.subdomain.example.com'

in some of my configuration files, so that object_url helpers (ActionView::Helpers::UrlHelper) produce links beginning with http://www.subdomain.example.com

I have tried to search the docs but I did not find anything except ActionMailer docs and http://api.rubyonrails.org/classes/Rails/Configuration.html which is not useful for me, because I do not know in which pat to look. Is there a place which describes the whole structure of Rails::Initializer.config?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
gorn
  • 5,042
  • 7
  • 31
  • 46

8 Answers8

59

asset_host doesn't work for urls

You need to override default_url_options in your ApplicationController (at least in Rails 3)

http://edgeguides.rubyonrails.org/action_controller_overview.html#default-url-options

class ApplicationController < ActionController::Base
  def default_url_options
    if Rails.env.production?
      {:host => "myproduction.com"}
    else  
      {}
    end
  end
end
tmr08c
  • 77
  • 1
  • 8
Wilhelm
  • 6,506
  • 5
  • 30
  • 26
  • 28
    Or you can set in your production.rb # production.rb config.action_controller.default_url_options = { host: 'myproduction.com' } – goma Feb 03 '15 at 03:57
  • hello, sorry to reopen it, but I have a quick question : can we put multiple addresses in :host value ? – user1713964 May 04 '16 at 08:46
  • Any updates as to how this might work in rails 5 engines? – Nick Res Apr 16 '18 at 20:18
  • Setting `config.action_controller.default_url_options = { host: 'myproduction.com' }` is not thread safe. It's recommended to override the method in ApplicationContorller, which is. – csalvato May 23 '19 at 19:42
54

Define the default host in your environment config:

# config/environments/staging.rb
MyApp::Application.configure do
  # ...
  Rails.application.routes.default_url_options[:host] = 'preview.mydomain.com'
  # ...
end

Then you can create a URL anywhere in your app:

Rails.application.routes.url_helpers.widgets_url()

Or include the URL helpers in your class:

class MyLib
  include Rails.application.routes.url_helpers

  def make_a_url
    widgets_url
  end
end

If you don't define the default host, you will need to pass it as an option:

widgets_url host: (Rails.env.staging? ? 'preview.mydomain.com' : 'www.mydomain.com')

It's also useful to specify things like the protocol:

widgets_url protocol: 'https'
Andrew
  • 227,796
  • 193
  • 515
  • 708
33

Another way is to set it like this

# config/production.rb
config.action_controller.default_url_options = { host: 'myproduction.com' }
goma
  • 955
  • 9
  • 15
4

In Rails 6.1 (at least), application-wide default_url_options can be set as follows:

# config/environments/development.rb
Rails.application.default_url_options = { host: 'localhost', port: 3000 }

Rails.application.configure do
 # ...
end

See: https://github.com/rails/rails/issues/29992#issuecomment-761892658

kkurian
  • 3,844
  • 3
  • 30
  • 49
3

You can easily set :host or/and :only_path parameter for every url_helper. yours_url(params, :host => "http://example.com", :only_path => Rails.env.test?) This way you are not setting global default_url_options in your environments, unless you want that.

0

As far as I know, the *_url helpers use the server's configured host name. So for example if my Apache installation is accepting requests for this Rails app at http://www.myapp.com/ then Rails will use that address. That's why the *_url methods in a development environment point to http://localhost:3000 by default.

The asset host suggested in the previous answer will only affect the image_tag, stylesheet_link_tag and javascript_link_tag helpers.

Robert Speicher
  • 15,382
  • 6
  • 40
  • 45
  • This is a good idea, but at least in my case it does not work. I have virtual server with ServerName subdomain.example.com and *_url helpers use only example.com. I have also tried to look at environment variables and I am getting HTTP_HOST as subdomain.example.com, so I am pretty sure that it is correct. – gorn Apr 17 '10 at 22:47
-5

NSD's solution is how I do it, but I had to add a block to make it work with https:

config.action_controller.asset_host = Proc.new { |source, request|
  (request ? request.protocol : 'http://') +  "www.subdomain.example.com"
}
simianarmy
  • 1,485
  • 10
  • 13
  • Asset host is for the asset pipeline, not the routes of the application. – rxgx May 22 '12 at 01:18
  • Who is NSD, and where did you place this code? I assume it was in either the application controller or one of the environment-specific files in /config/environments. As this is a site which is provided to help people solve problems, clarity is very important. – Tass Oct 07 '13 at 16:23
  • NSD was a commenter who's post has been removed. – simianarmy Oct 07 '13 at 20:32
-10

There's this, but I'm not terribly sure if they're the helpers you're referring to:

ActionController::Base.asset_host = "assets.example.com"

http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html

Azeem.Butt
  • 5,855
  • 1
  • 26
  • 22
  • I mean helpers as in http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html I have added this to my question. – gorn Apr 17 '10 at 22:48
  • 2
    `asset_host` deals with the host for the asset pipeline, not the application's routes. – rxgx May 22 '12 at 01:19