Ruby on Rails 4:如何在Rails Web应用程序中包含Javascript文件? [英] Ruby on Rails 4: How to include Javascript files in Rails web application?

查看:90
本文介绍了Ruby on Rails 4:如何在Rails Web应用程序中包含Javascript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个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 路径,所以你不必自己输入(你不应该,或者你的文件包括两次)。默认情况下包含JQuery库(它来自jQuery gem)。如果您想要Bootstrap,那么您可以这样做或使用现有的宝石之一,如 bootstrap-generators twitter-bootstrap-rails

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆