Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 4 asset pipeline image subdirectories

I know this is probably an easy question but I am stumped here.

The application I am working on houses assets like so:

app
--assets
----fonts
----images
----javascripts

I like to organize assets efficiently to avoid a mess down the road so I am trying to break up images like so:

app
--assets
----fonts
----images
------icons
------views
--------home
--------admin

Ideally I would like to reference images like image.png without having to add the folder path in front of the asset like views/home/image.png which I believe has to be possible although not setup like that out of the box.

like image 880
Chris Hough Avatar asked Feb 01 '14 19:02

Chris Hough


People also ask

How do you Precompile rails assets?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What is assets Precompile?

rails assets:precompile is the task that does the compilation (concatenation, minification, and preprocessing). When the task is run, Rails first looks at the files in the config.assets.precompile array. By default, this array includes application.js and application.css .

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.


2 Answers

It's possible if you manually add all paths underneath app/assets/images to the Rails asset paths in your application.rb:

Dir.glob("#{Rails.root}/app/assets/images/**/").each do |path|
  config.assets.paths << path
end
like image 52
Marcelo De Polli Avatar answered Oct 23 '22 20:10

Marcelo De Polli


In Rails 4+ any changes to asset paths should be made in:

config/initializers/assets.rb

To add all subdirectories in app/assets/images to the path, add the following:

Dir.glob("#{Rails.root}/app/assets/images/**/").each do |path|
  Rails.application.config.assets.paths << path
end

Afterwards, you can verify the asset paths in the rails console with the following:

Rails.application.config.assets.paths.each do |p|
  puts p
end
like image 45
Eribos Avatar answered Oct 23 '22 18:10

Eribos