Random Images in your Rails App

January 17, 2008

 
No Gravatar

Ever want to get a random image from a folder in your rails app? Well, here you have it…place this in your application helper:

1
2
3
4
5
6
7
8
9
10
11
12
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
 
  def random_header_image
    image_files = %w( .jpg .gif .png )
    files = Dir.entries(
          "#{RAILS_ROOT}/public/images/random_image_folder"
      ).delete_if { |x| !image_files.index(x[-4,4]) }
    files[rand(files.length)]
  end  
 
end
blog comments powered by Disqus