How can I configure rails to serve assets on different subdomain? I basically want view/assets helpers to use subdomain for all static files, such as;
do you know about the asset_host
option?
# config/environments/production.rb
config.action_controller.asset_host = "static.example.com"
it's also possible to do dynamic names:
ActionController::Base.asset_host = Proc.new { |source|
"http://assets#{Digest::MD5.hexdigest(source).to_i(16) % 2 + 1}.example.com"
}
http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html
You can also try the rack-cors
gem for Cross-Origin Resource Sharing. https://github.com/cyu/rack-cors
I've used this gem in a Rails 4 app when my font-awesome icons wouldn't render once I started using subdomains. This wiki put me on the right path: https://github.com/bokmann/font-awesome-rails/wiki/CORS-Cross-Domain-Support
Besides modifying my Gemfile, I also put the following code into config/application.rb
toward the top according to this guide: https://github.com/cyu/rack-cors/blob/master/examples/rails4/config/application.rb
config.middleware.insert_before 0, "Rack::Cors", :debug => true, :logger => (-> { Rails.logger }) do
allow do
origins '*'
resource '/cors',
:headers => :any,
:methods => [:post],
:max_age => 0
resource '*',
:headers => :any,
:methods => [:get, :post, :delete, :put, :patch, :options, :head],
:max_age => 0
end
end
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