Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use the Asset Pipeline to serve images?

In the Ruby on Rails guide to the Asset Pipeline, it says

Any assets under public will be served as static files by the application or web server. You should use app/assets for files that must undergo some pre-processing before they are served.

http://guides.rubyonrails.org/asset_pipeline.html

To me, this says that images should be kept in the public directory as they can be served statically by my web server and require no pre-processing.

Are there advantages to putting your images in assets/?

like image 595
gmoore Avatar asked Jun 08 '12 16:06

gmoore


People also ask

What is an asset pipeline?

1 What is the Asset Pipeline? 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 and pre-processors such as CoffeeScript, Sass, and ERB.

What does Rails assets Precompile do?

The Rails asset pipeline provides an assets:precompile rake task to allow assets to be compiled and cached up front rather than compiled every time the app boots. There are two ways you can use the asset pipeline on Heroku. Compiling assets locally. Compiling assets during slug compilation.

What are assets in Rails?

Generally asset is anything that browser loads after it gets the HTML page. Meaning javascript, css and any images. But as you pointed out there are two different image types in a rails project.


2 Answers

Asset precompile appends unique hash value to image filenames, which allows users to get latest version of it despite of cache or expire settings on the server. This is useful when you want to change images in website design.

You don't want to use /assets/images for images what are unlikely to change (like user uploads).

like image 148
Krists Avatar answered Oct 16 '22 22:10

Krists


Because it would harm the versatility of the asset pipeline.

With images being served via the asset pipeline, if you change the asset pipeline (for instance, have it upload files to S3) you would have to then sync your images via some other task.

There may be other, deeper reasons, but that is what would give me pause.

EDIT: Side note: In production thanks to assets:precompile everything from /assets/ will be served from public.

like image 38
Daniel Evans Avatar answered Oct 16 '22 22:10

Daniel Evans