Created
August 10, 2015 18:19
-
-
Save supairish/27215e50d9a914c56c5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hey! | |
Here is what I currently use: | |
Put this in your application helper: | |
def image_assets | |
image_asset_dir = Rails.root.join('app', 'assets', 'images') | |
assets = {} | |
%w(.jpg .png .jpeg .svg).each do |file_ext| | |
Dir.glob(File.join(image_asset_dir, "**", "*#{file_ext}")).each do |absolute_path| | |
file = absolute_path.sub(File.join(image_asset_dir, '/'), '') | |
assets[file] = asset_url(file) | |
end | |
end | |
assets | |
end | |
Add this to you /app/assets/javascripts/assets.js.erb: | |
<% environment.context_class.instance_eval { include ApplicationHelper } %> | |
window.assets = window.assets || {}; | |
window.assets.images = <%= raw(image_assets.to_json) %>; | |
Create a helper: | |
return window.assets.images[params[0]]; | |
then you can do something like this in your template: | |
<img src="{{asset-path 'login.png'}}"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment