Reputation: 3628
development.rb:
config.action_controller.asset_host = "assets.myserver.com"
view script:
<%= image_tag('header.jpg') %>
yields:
<img alt="Header" src="/header.jpg" />
should be:
<img alt="Header" src="http://assets.myserver.com/header.jpg" />
I am using the rails-api
gem which I am guessing disables some asset and view rendering stuff.
It seems like it should not be too hard to re-implement (override image_tag
) to add this very small feature. It may seem a little odd to want to do this. However, I am new-ish to rails would like to know how to do this as a learning experience.
Questions:
Upvotes: 6
Views: 2043
Reputation: 2966
I've tried your configuration, but when I use config.action_controller.asset_host = "assets.myserver.com"
in my development.rb
image_tag
works as expected:
<img alt="Header" src="http://assets.myserver.com/assets/header.jpg" />
I've tested it both under Rails 3.2.7 and 3.2.8, but it works in both versions.
UPDATE
In my original answer I didn't use the rails-api
gem. When using the rails-api
gem image_tag
works as described in the question.
Well to answer the actual question, you could add an initializer in config/initializers
. Just create a file, lets say image_tag_helper.rb
, with the following code:
# config/initializers/image_tag_helper.rb
module ActionView
module Helpers
module AssetTagHelper
def image_tag(source, options = {})
options[:src] = "http://#{source}"
tag("img", options)
end
end
end
end
What this basically does is reopening the module and replace the image_tag
method with your own method. All other methods within the module AssetTagHelper
remain the same. Take a look at the Rails repository at github for a complete 'example' of the image_tag
method.
The name of the file doesn't really matter. All files within config/initializers
are loaded when the application is booted.
Basically this is a language feature of Ruby, Ruby allows you to reopen classes everywhere in your code and add or replace methods (you find more on this subject at rubylearning.com).
Upvotes: 3