stick's corner

All composite phenomena are impermanent. All contaminated things and events are unsatisfactory.

Image_url Function in Ruby on Rails

| Comments

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:

1
2
3
4
5
6
7
  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)

Comments