---
title: "rails 生成jquery   ajax"
author: "yangbinfc"
date: "2011-03-02T13:38:00Z"
tags: []
slug: "rails-jquery-ajax"
draft: false
lastmod: "2012-11-04T17:47:37Z"
id: 1715354
url: "https://yangbinfc.iteye.com/blog/1715354"
---

1：rails new sample -T 不生成测试目录  
2：生成jqeury自动移除protype：

rails new hel -J  
gem "jquery-rails"  
bundle install  
rails g jquery:install 可以加上: 或者--ui  
3: rails 3用mysql 加上gem mysql2" 修改yml文件

```
rails3中 不适用link_to_remote form_remote：





#==========

```

**1. 让Rails3使用jQuery**

轻量级js框架jquery相信大家一定不会陌生, 本教程中的ujs教程也是基于jquery来写的(当然你也可以使用prototype).

首先, 下载最新的jquery然后放入 public/javascripts 文件夹下面:

$curl -L http://code.jquery.com/jquery-1.4.3.min.js &gt; public/javascripts/jquery.js

同时我们也需要把原来的prototype ujs的rails.js 改成 jquery-ujs的rails.js:

$ curl -L http://github.com/rails/jquery-ujs/raw/master/src/rails.js &gt; public/javascripts/rails.js

下载后可以看下javascripts文件夹, 里面有application, controls, dragdrop, effects, jquery, prototype 以及 rails 等js文件, 出来jquery以外, 其他都是生产bolg应用时rails加进去的.

现在打开 config/application.rb 文件, 修改下面的配置:

Ruby代码

1. #JavaScriptfilesyouwantas:defaults(application.jsisalwaysincluded).
2. config.action\_view.javascript\_expansions\[:defaults ]=%w(jqueryrails)

\# JavaScript files you want as :defaults (application.js is always included). config.action\_view.javascript\_expansions\[:defaults] = %w(jquery rails)

上面的代码告诉rails, :default 符号的定义现在改成 jquery和rails啦, 启动我们的博客程序, 查看源代码中的&lt;head&gt;中的&lt;script&gt;是不是这3个js脚本.

#==========

```



```

rails2：  
link\_to\_remote 'delete', :url =&gt; admin\_image\_path(image), :confirm =&gt; 'Are you sure you want to delete this image?', :method =&gt; :delete  
rails3：  
link\_to "Delete", admin\_image\_path(image), :method=&gt;:delete, :confirm=&gt;"Are you sure you want to delete this image?", **:remote=&gt;:true, :class=&gt;"alert"**