I want Rails 3 to dynamically grab assets depending on current domain:
mysite.com - assets.mysite.com mysite.ru - assets.mysite.ru
Is it possible out of the box? Or I should implement it manually?
Try sending a proc to asset_host, which yields both the asset path and request:
config.action_controller.asset_host = proc {|path, request|
"http://assets." << request.host
}
Note the key differences from other solutions in the solutions below:
I have always done something like this:
config.action_controller.asset_host = Proc.new { |source, request|
"http#{request.ssl? ? 's' : ''}://cdn#{(source.hash % 3)}." << request.domain # cdn0-3.domain.com
}
Or if you have multiple asset/cdn hosts you could decide to be selective about what kind of assets are served from what host like so:
config.action_controller.asset_host = Proc.new { |source, request|
case source
when /^\/images/
"http#{request.ssl? ? 's' : ''}://cdn#{(source.hash % 2)}." << request.domain # cdn0-1.domain.com
when /^\/javascripts/, /^\/stylesheets/
"http#{request.ssl? ? 's' : ''}://cdn#{(source.hash % 2) + 2}." << request.domain # cdn2-3.domain.com
else
"http#{request.ssl? ? 's' : ''}://cdn4." << request.domain # cdn4.domain.com
end
}
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With