Skip to content

Instantly share code, notes, and snippets.

@2get
Created May 20, 2016 06:45
Show Gist options
  • Save 2get/2e49cb97f6612a57174de0183a1fed88 to your computer and use it in GitHub Desktop.
Save 2get/2e49cb97f6612a57174de0183a1fed88 to your computer and use it in GitHub Desktop.
Reference Rails asset images in JS
<%
imgs = {}
Dir.chdir("#{Rails.root}/app/assets/images") do
imgs = Dir.glob("**/*.{gif,jpg,jpeg,png}").inject({}) { |h, f| h.merge! f => image_path(f) }
end
%>
window.image_path = function(name) {
return <%= imgs.to_json %>[name];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment