Home > Blog > image_url function in Ruby on Rails

image_url function in Ruby on Rails
1 star2 stars3 stars4 stars5 stars
(no votes yet)
Loading ... Loading ...

December 23rd, 2009 Leave a comment Go to 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:

  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)

  • Twitter
  • Facebook
  • Identi.ca
  • LinkedIn
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • Netvibes
  • Ping.fm
  • Slashdot
  • StumbleUpon
  • Technorati
  • Tumblr
Tags: ,
  1. joserwan
    December 29th, 2009 at 20:38 | #1 | Firefox 3.5.6Ubuntu 9.10
  2. December 29th, 2009 at 22:50 | #2 | Google Chrome 4.0.276.0GNU/Linux

    @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.

  1. No trackbacks yet.
or