比如某个 js 文件只在某个不常用的页面使用,放在 layouts 里面不是不管用不用都要加载它?
在 layout 的头部,增加
<html>
<head>
  ...
  <%= yield :head_block %>
</head>
<body>
  ...
</body>
然后在那个不常用的页面,用content_for来加载某个 js:
<% content_for :head_block do %>
  <%= javascript_include_tag 'some.js' %>
<% end %>
          controllers/application_controller.rb
class ApplicationController < ActionController::Base
  def self.page_javascripts(*files)
    files.empty? ? @page_javascripts : @page_javascripts = files
  end
  def page_javascripts
    @page_javascripts || self.class.page_javascripts
  end
end
controllers/welcome_controller.rb
class WelcomeController < ApplicationController
  # for all actions
  page_javascripts 'welcome'
  def index
    # override for only this action
    @page_javascripts = ['welcome_index']
  end
end
views/layouts/application.html.erb
<head>
  ...
  <%= javascript_include_tag "application" %>
  <%= javascript_include_tag *controller.page_javascripts if controller.page_javascripts %>
  ...
</head>
          layout_helper.rb
def stylesheet(*args)
  content_for(:head) { stylesheet_link_tag(*args) }
end 
def javascript(*args)
  content_for(:head) { javascript_include_tag(*args) }
end
layouts/application.haml
= stylesheet_link_tag    "application"
= javascript_include_tag "application"
= yield :head
report/show.haml
- javascript "backbone_bundle", "highcharts", "custom_highchart"
          @naitnix require_tree 就是加载全部 js。 看你的需求定了。 如果你不想把个别比较大的 js 放在全局里,那就 在页面中 用我写的 javascript 方法,并且在 config.assets.precompile 要加上该文件名。
如果你的 js 没有命名冲突也比较小 就在 application.js 中 require 就行。
#9 楼 @yakjuly thanks very much 我看了这篇文章http://gogojimmy.net/2012/07/03/understand-assets-pipline/,再加上你的方法会更灵活一些