How can I include specific JS or CSS files (by convention ?) with Ruby Rails 3.1 ?
I have a view :
views/project/index.html.erb
And I want to include a specific javascript file for this page. I put it in
assets/javascripts/project/index.js
Same for another view :
home/index.html
Thanks
In the
application.cssandapplication.jsfile, be sure to remove the line\\= require tree.Then, manually list all the css/js files you want included in each manifest file, for example:
Then, I would setup a yield in your header or your closing body tag for your application layout file, for instance (in haml)
Then in your particular view, say
_example_partial.html.haml, do this:You do the exact same thing with Javascript files, just using
javascript_include_taginstead ofstylesheet_link_tag.This will let you quickly and easily assemble view-specific javascript / css payloads. There may be a more sophisticated way to handle this using the asset pipeline, but I would suggest that if the asset pipeline is already minifying and merging you major stylesheets that this kind of +1 css / js file per view is not going to cause a major performance hit. Just try to make sure you don’t overdo it with dozens of separate files loading into a single view.