Rails 3.1 新手:我应该把 javascript 代码放在哪里? [英] Rails 3.1 newbie : where should I put javascript code?

查看:75
本文介绍了Rails 3.1 新手:我应该把 javascript 代码放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Rails 3.1 应用程序,我对应该将自己的 javascript 代码(例如 my.js)放在哪里以及将 3rd-party javascript 放在哪里感到困惑库(例如 jQuery-UI).

I am developing Rails 3.1 application, I get confused about where should I put my own javascript code(e.g. my.js) and where to put 3rd-party javascript library (e.g. jQuery-UI).

我知道在旧版本的 Rails 中,javascript 应该全部进入 public/javascripts/ 目录,当我生成 Rails 3.1 应用程序时,没有 public/javascripts/文件夹,但有 app/assets/verndor/assets/app/assets 中有 application.js强>,请问:

I know in the old version Rails, javascript should all goes into public/javascripts/ directory, when I generate Rails 3.1 app, there is no public/javascripts/ folder, but there is app/assets/ and verndor/assets/ and there is application.js in app/assets, I would like to ask:

  1. 在 Rails 3.1 中,我应该将 my.jsjQuery-UI js 放在 Rails 3.1 中的什么位置??
  2. app/assets/application.js 应该做什么?
  3. 如何在我的 html 页面中包含 my.jsjQuery-UI js?
  1. in Rails 3.1 ,where should I put my.js and jQuery-UI js in Rails 3.1??
  2. what is the app/assets/application.js supposed to do?
  3. how can I include my.js and jQuery-UI js in my html page?

----------------我说得对吗?----------

application.js 中的 require_tree 是否用于在 app/vendor/assets/javascript/ 下包含第三方库

Is require_tree in application.js is used to include 3rd-party libraries under app/vendor/assets/javascript/

require "something"application.js中用于在app/assets/javascripts/下包含js文件??我说得对吗?

and require "something" in application.js is used to include js file under app/assets/javascripts/?? Am I right?

推荐答案

把你自己的 javascript &app/assets/javascripts 下的 coffeescript.

Put your own javascript & coffeescript under app/assets/javascripts.

看一看 app/assets/javascripts/application.js.当您运行 rails new APP 时,它应该已将 //= require_tree . 添加到此文件中.更多信息见第 2.3 节.

Take a look inside app/assets/javascripts/application.js. When you ran rails new APP it should have added //= require_tree . to this file. See section 2.3 for more.

这是Sprockets 理解的特殊说明将自动将所有文件包含在与您的 application.js 文件相同的目录中以及它下面的子文件夹中.

This is a special instruction that Sprockets understands that wil automatically include all files in the same directory as your application.js file and in subfolders below it.

如果你想加载 jquery 和 jquery-ui,你的 application.js 文件应该是这样的

If you want jquery and jquery-ui to be loaded your application.js file should look like

//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree .

如果你的包中有 gem 'jquery-rails',这个 gem 有已经为您将 jquery 文件添加到资产管道中,因此您不必担心手动下载它们.

在应用程序视图的 head 部分,您需要在 head 部分包含 javascript_include_tag "application".很有可能,如果您使用过生成器,它已经在那里了.

In your head section of your application view you will want to include javascript_include_tag "application" in the head section. Chances are, if you used the generator, it's already there.

请阅读 Rails Asset Pipeline 了解更多信息.

Please read up on the Rails Asset Pipeline for more information.

require_tree 一起的行中的 . 引用当前文件的位置.//= 运算符正在引用资产管道.

The . in the line with require_tree is referencing the location of the current file. The //= operator is referencing the asset pipeline.

将文件放在 app/assets、lib/assets 或 vendor/assets 中会将其添加到资产管道中.参见第 2.1 节.

Placing a file in app/assets, lib/assets or vendor/assets will add it to the asset pipeline. See section 2.1.

这篇关于Rails 3.1 新手:我应该把 javascript 代码放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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