On the topic of the asset pipeline, Rails Guides suggest that Rails can link to controller specific CSS files simply by calling:
stylesheet_link_tag params[:controller]
The excerpt from Rails Guides:
For example, if you generate a ProjectsController, Rails will also add a new file at app/assets/javascripts/projects.js.coffee and another at app/assets/stylesheets/projects.css.scss. You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these controllers with lines 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 just fine in development where we allow Rails to fall back on the asset pipeline. In production, however, I get an error saying that the stylesheet isn't precompiled.
From what I've read, you have to add any assets that you want to be manifested as independent files to the precompile array like this:
config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
If I want controller specific stylesheets that are linked as per the Rails Guide example above, do I have to enumerate each one in the precompile array?
The short answer is... Yes.
However, remember that the asset pipeline can also be used to match selectors. I implemented a similar setting for a simple cms where I knew I wanted each controller to have its own .css and .js. The config looks like...
config.assets.precompile += ["*.css", "*.js")
Not the best solution if you have allot of stylesheets or scripts that aren't directly related to controllers. In my case each controller needed very different .js files.
Also, if your different assets can be grouped into two or three inter-related groups, you might consider creating multiple manifests and then listing them explicitly.
EDIT I would like to round out this answer for anyone who is trying to decide how to set up their asset precompiling.
Along with explicitly precompiling each asset or manifest, you can also put all files you want precomiled into a folder in your assets folder and use the same wildcard matching to compile only those assets.
for example
config.assets.precompile += ['*.css', 'manifests/*.js']
would compile all css sheets and only .js files in the manifests folder.
Similarly you can pass assets.precomile a regex. So if you wanted to append "pre_" to any file you wanted precomiled you could use a match like...
config.assets.precompile += [/^pre_\w*.(css|js)/i]
The possibilities are endless, just don't feel you have to settle for precompiling all assets, a probably useless en devour since most scripts and css should be written to be used in multiple instances and in conjunction with other scripts, or throwing everything, even highly specialized scripts into application.* .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With