Rails3 Resource Pipeline: Custom Controller Style Sheets

On the topic of the resource pipeline, Rails Guides suggests that Rails can reference CSS files with a controller simply by calling:

stylesheet_link_tag params[:controller] 

Excerpt from Rails Guides:

For example, if you create a ProjectController, Rails will also add a new file to app / assets / javascripts / projects.js.coffee, and another to app / assets / stylesheets / projects.css.scss. You must place any JavaScript or CSS unique to the controller inside your respective asset files, as these files can only be loaded for these controllers with strings such as <% = javascript_include_tag params [: controller]%> or <% = stylesheet_link_tag params [: controller]%>. http://guides.rubyonrails.org/asset_pipeline.html#how-to-use-the-asset-pipeline

Thats works very well in development, where we allow Rails to return to the asset pipeline. However, in production, I get a message that the stylesheet is not precompiled.

From what I read, you need to add any assets that you want to map as independent files to the precompilation array as follows:

 config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js'] 

If I need specific controller stylesheets that are linked according to the Rails example above, do I need to list each of them in a precompilation array?

+6
source share
1 answer

Short answer: Yes.

However, remember that the asset pipeline can also be used to match choices. I applied a similar setting for simple cms, where I knew that each controller should have its own .css and .js. The configuration looks like ...

 config.assets.precompile += ["*.css", "*.js") 

Not the best solution if you have style sheets or scripts that are not directly related to controllers. In my case, each controller needed very different .js files.

In addition, if your different assets can be grouped into two or three related groups, you can consider creating multiple manifests, and then explicitly specify them.

EDIT I would like to complete this answer for those who are trying to decide how to set up pre-compilation of their assets.

In addition to explicitly precompiling each asset or manifest, you can also put all the files you want to precompile in a folder in the folder with your assets and use the same template as for compiling only these assets.

eg

 config.assets.precompile += ['*.css', 'manifests/*.js'] 

will compile all css lists and only .js files in the manifestests folder.

Similarly, you can pass the assets.precomile regular expression. Therefore, if you want to add "pre_" to any file that you want to compile, you can use a match like ...

 config.assets.precompile += [/^pre_\w*.(css|js)/i] 

The possibilities are endless, just don’t feel that you have to be content with pre-compiling all the assets, probably useless en devour, since most scripts and css should be written for use in several instances and in combination with other scripts, or throw everything, even highly specialized scripts in attachment. *.

+6
source

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


All Articles