| image_url function in Ruby on Rails |
If you need to get the full URL of an image, just put the following code snippet into ApplicationHelper module in your app/helpers/application_helper.rb:
def image_url(source)
abs_path = image_path(source)
unless abs_path =~ /^http/
abs_path = "#{request.protocol}#{request.host_with_port}#{abs_path}"
end
abs_path
end
I wonder why this function is not already a standard part of Rails.
(Idea by Rob Biedenharn)


See image_path(source) : http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#M001720
@joserwan
I know about image_path. It is even used in the code! The problem is that image_path does not always create a full URL.
Could you help me please? I cannot use this in email layout, somehow…
in application_helper.rb I have
def image_url(source)
abs_path = image_path(source)
unless abs_path =~ /^http/
abs_path = “#{request.protocol}#{request.host_with_port}#{abs_path}”
end
abs_path
end
and in my email model (order_mailer.rb) I have
class OrderMailer < ActionMailer::Base
layout 'emails'
. . .
end
but when I use I get
undefined method `image_url’ for #
This doesn’t work either:
class OrderMailer < ActionMailer::Base
include ApplicationHelper
layout 'emails'
. . .
end