How can I include specific JS or CSS files (by convention ?) with Ruby Rails 3.1 ?
I have a view : views/开发者_如何学Pythonproject/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.css
and application.js
file, 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:
// application.js
//= global.js
//= everywhere.js
Then, I would setup a yield in your header or your closing body tag for your application layout file, for instance (in haml)
%head
%title Some Page
= stylesheet_link_tag 'application'
= yield :stylesheets
Then in your particular view, say _example_partial.html.haml
, do this:
- content_for :stylesheets do
= stylesheet_link_tag 'example_partial'
-# the rest of your view goes here
You do the exact same thing with Javascript files, just using javascript_include_tag
instead of stylesheet_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.
精彩评论