Ruby on Rails 4:如何在 Rails Web 应用程序中包含 Javascript 文件? [英] Ruby on Rails 4: How to include Javascript files in Rails web application?
问题描述
我正在构建一个 Rails 4 Web 应用程序,我想在我的应用程序中包含一些 .js 文件.是否可以将 JavaScript 文件直接添加到我的 Rails ..app/assets/javascripts
文件夹并像这样在 application.js 中添加引用?
I am building a Rails 4 web application and I want to include some .js files into my application. Is it possible to directly add JavaScript file to my Rails ..app/assets/javascripts
folder and add reference in application.js like this?
//= customejsfile.js
这是正确的方法吗?如果是,我是否可以按照相同的步骤添加 jQuery 和 Bootstrap 库?
Is this the right way? If yes, is it possible for me to follow the same steps in adding jQuery and Bootstrap library?
感谢任何帮助.
推荐答案
包含文件的正确方法是:
The right way to include your files is:
//= require customejsfile
在您的 application.js
文件中.更重要的是,默认情况下你有
in your application.js
file. What's more, by default you have
//= require_tree .
它需要 assets/javascript
路径中的所有 js 文件,所以你不必自己输入它(你不应该,否则你的文件会包含两次).默认情况下包含 JQuery 库(它来自 jQuery gem).如果你想要 Bootstrap,那么你可以这样做或者使用现有的 gems 之一,比如 bootstrap-generators 或
which requires all js files from assets/javascript
path, so you don't have to type it on your own (and you shouldn't, or you'll have your file included twice). JQuery library is included by default (and it comes from jQuery gem). If you want Bootstrap, then you can do it this way or use one of existing gems, like bootstrap-generators or twitter-bootstrap-rails.
这篇关于Ruby on Rails 4:如何在 Rails Web 应用程序中包含 Javascript 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!