Rails Asset Pipeline with Sub

I have begun using the Rails Asset Pipeline in my application. It is working great for stylesheets and javascripts but I cannot get my images to be returned successfully. I think I understand why but I don't know how to fix it.

My application using subdomains: bob.myapp.com/, mandy.myapp.com/, etc. When I put an image_tag link within a view, eg link_to image_tag('remove.png'...) it is rendered with the HTML attribute src="/assets/remove.png" . Reasonable, however, this means the location that the browser is looking is: http://bob.myapp.com/assets/remove.png

If I paste that directly into my address bar, it is not found. If I change the subdomain to http://www.myapp.com/assets/remove.png (ie switching bob to www ) the image is retrieved successfully.

So, how do I force my image_tag to seek a source from www or can I somehow make the asset pipeline work off of all subdomains by default?

Rails 3.1.0 Ruby 1.9.2p0


You might want to set the host of assets in your config/environments/production.rb (or any other environments you defined).

For example:

MyApp::Application.configure do
  config.action_controller.asset_host = "http://www.myapp.com"
end

Since it accepts a Proc , you can do some computation there and set different hosts for assets:

MyApp::Application.configure do
  config.action_controller.asset_host = Proc.new do |source|
    subdomain = compute_subdomain(source)
    "http://#{subdomain}.myapp.com"
  end
end

The image_tag(source, options) helper will respect it and output full url for assets.

Although in my humble opinion, I suggest placing all assets on every asset server, that way there is no need to set this.

链接地址: http://www.djcxy.com/p/79014.html

上一篇: Rails 4:资产没有在生产中加载

下一篇: Rails资产管道与子