Rails 3.1 资产管道:加载特定于控制器的脚本?
问题说明
如果我在 Rails 3.1 中生成一个新的控制器,也会自动添加一个带有控制器名称的 javascript 文件.首先,我认为这个javascript文件只会在调用相关控制器时使用.
If I generate a new controller in Rails 3.1, also a javascript file with the name of the controller will added automatically. Firstly, I thought this javascript file will used only, when the related controller is called.
默认情况下,application.js
文件中有 //= require_tree .
指令,其中包含其树上的每个 javascript 文件.
By default there is the instruction //= require_tree .
in the application.js
-file, that include every javascript file on it's tree.
如何只加载控制器特定的脚本?
How could I load only the controller specific script?
正确答案
只加载必要的 name_of_the_js_file.js 文件:
To load only the necessary name_of_the_js_file.js file:
-
从
application.js
将您的 js 文件(您希望在加载特定页面时加载)保留在资产管道中
keep your js file (that you want to load when a specific page is loaded) in the asset pipeline
在application_helper.rb
def javascript(*files)
content_for(:head) { javascript_include_tag(*files) }
end
-
屈服于您的布局:
-
yield into your layout:
<%= yield(:head) %>
-
在您的视图文件中添加:
-
add this in your view file:
<% javascript 'name_of_the_js_file' %>
那应该没问题
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcbbkkg
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20