最近在项目中使用的一些Javascript编辑器比较大,然而只是在单个页面中使用到. 集成到Asset Pipline中增加了页面的加载的内容会比较大, 于是想到了按需加载.
在Application Helper中添加方法:
def render_javascripts
if controller_name == 'controller' and action_name == 'action'
@content = content_tag('script', nil, { src: '\js\bigfile.js' }, false
@content << content_tag('script', nil, { src: '\js\bigfile2.js' }, false
end
end
然后在Application.html.erb中添加即可.
<%= render_javascripts %>