views:

165

answers:

3

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 link beginning with http://www.subdomain.example.com

I have tried to search the docs but I did not find anytnig exept ActionMailer docs and http://api.rubyonrails.org/classes/Rails/Configuration.html which is not usefull 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?

Thanks for helping Jakub

A: 

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
I mean helpers as in http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.htmlI have added this to my question.
gorn
A: 

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.

rspeicher
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
I'm failing to reproduce what you're talking about locally.smackaho.st is a special domain that always points to 127.0.0.1, so you can simulate subdomains on your local development server. I start a local server like so: `./script/server -p 3030 -P /path -e production` and then go to `http://subdomain.smackaho.st:3030/path/foo` which presents me with this:`root_url is http://subdomain.smackaho.st:3030/path/``root_path is /path/``foo_index_url is http://subdomain.smackaho.st:3030/path/foo`Seems fine to me.
rspeicher
A: 

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