📜 ⬆️ ⬇️

Asset Pipeline Recipe

The post was inspired by long testing of various hypotheses of correct work with images through the Asset Pipeline after switching to Ruby on Rails 3.1 from previous versions.

So, the Task


In the CSS file, specify the image located in the assets folder. The image in the production environment should be precompiled.
It would seem, well, there is such an official guide , an adapted translation of the guide , a screencast , in the end. Here the rule “It just works” works only if you did not allow your (or others) hands to sensitive points. Which ones?

Decision


During the experiments, only one hypothesis was confirmed, which, for the convenience of presentation, is convenient to be divided into several parts.
The tree of files will look like this (.erb is a whim, work through the sass-rails engine is also possible):

app /
... assets /
...… images /
.......... rails.png
…… stylesheets /
……… application.css.erb
')
1. For precompilation we use not rake assets: precompile, but a slightly altered rake task
Periodically, I had an error
rake assets:precompile
rake aborted! rails.png isn't precompiled

The reason for her appearance is incomprehensible to me, but I know for sure that I am not the only one: the solution to this problem was found on the githaba .

2. To specify the path in css, we use the asset_path erb-template engine helper.
There are a lot of helpers for SASS , but none of them worked in my project, I do not use it yet.
Content application.css.erb:
#header { background-image: url(<%= asset_path("rails.png") %>)

In case of using sass (application.css.scss):
#header { background-image: image_url('rails.png') }

3. Settings production.rb, which also play a role:
config.assets.compile = false #
config.assets.digest = true # ( )
config.action_dispatch.x_sendfile_header = "X-Sendfile" # 'X-Accel-Redirect' nginx, , http-server

The last line strained me a lot when running on a local machine:
The image “localhost: 3000 / assets / rails-s43o54m765t656ed76i8gest.png” cannot be displayed because it contains errors.
The problem was that the Webrick's http-server does not support the x_sendfile_header directive (perhaps I am mistaken, but something definitely did not work there).

Conclusion


Of course, I have not yet figured out the topic until the end, but, as Alexander Sergeyevich put it,
Oh, how many wonderful discoveries to us
Prepare an enlightened spirit

UPD : created a project on github , demonstrating work using sass-rails

Source: https://habr.com/ru/post/127688/


All Articles