Skip to content

Instantly share code, notes, and snippets.

@kristjan
Created November 17, 2010 00:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristjan/702789 to your computer and use it in GitHub Desktop.
Save kristjan/702789 to your computer and use it in GitHub Desktop.
Helpers to use Rails image_paths in CSS background images
module AssetsHelper
def background_images(images)
content_for :head do
content_tag(:style, :type => 'text/css') do
images.map{|selector, path| css_background_image(selector, path)}.
join("\n")
end
end
end
def css_background_image(selector, given_path)
c_a = "#{params[:controller]}/#{params[:action]}"
path = given_path =~ %r{/} ? given_path : "#{c_a}/#{given_path}"
<<-CSS
#{selector} {
background-image: url('#{image_path(path)}');
}
CSS
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment