- 浏览: 172063 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (174)
- rails (25)
- js (15)
- ruby (30)
- webserver (5)
- mysql (13)
- security (5)
- thinking (5)
- common sense (2)
- linux (18)
- android (26)
- web browser (1)
- config and deploy (1)
- mac (5)
- css (2)
- db (8)
- version manager (1)
- editor (1)
- job (1)
- OOA (1)
- php (1)
- apache (2)
- mongrel (1)
- Mongodb (1)
- facebook (1)
- 架构 (1)
- 高并发 (1)
- twitter (1)
- Erlang (1)
- Scala (1)
- Lua (1)
- ubuntu (3)
- cache (1)
- 面试题 (2)
- android layout (2)
- android控件属性 (2)
- java (5)
- customize view (1)
- advanced (2)
- python (2)
- 机器学习 (5)
最新评论
Changing from Prototype to Jquery
For default Rails implements ajax requests with prototype, we need to change it to work with jquery.First, download the rails.js equivalent with jquery from
github
Copy the rails.js under the src folder to your public/javascripts/.In our layout "application.html.erb" we'll change the next lines <%= javascript_include_tag :defaults %> to <%= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js' %>
It's time to the ajax magic
Keep it simple, let's think about what we want: So let's start changing everything again. The template(layout) need to contain a div to show the flash_notice: <body> Put some partials in our index and add the remote => true in our links and forms:
<% title "Posts" %> /views/posts/_form.html.erb
I also put a div to show the errors here, so we can change it in our js responses: <%= form_for(@post, :remote => true) do |f| %> /views/posts/_posts.html.erb
I created this partial so i can update the list of posts with javascript too: <table> We need to change our controller too. Since we gonna make a 100% ajax CRUD we don't need that redirects anymore:
class PostsController < ApplicationController
Almost there. If you try to use the form now, nothing happens right? If you take a look at the console(server) you will find some errors telling you Rails can't find the view js to render. So let's create this files. Basically we gonna create a view to render javascript, with this javascript we gonna inject or modify our html. We are using jquery to do the modification/injection part, so if you never work with that take a look at the official site to learn a lot of cool stuffs: http://www.jquery.com We need files to create, edit, update and destroy actions, so let's create them: /views/posts/create.js.erb
Here we verify if the @post object contains errors and changes the behavior according to that: <% if @post.errors.any? -%> /views/posts/edit.js.erb
In this action we just need to update the form with the select post. $("#post_form").html("<%= escape_javascript(render(:partial => "form"))%>"); /views/posts/update.js.erb
<% if @post.errors.any? -%> /views/posts/destroy.js.erb
<%= csrf_meta_tag %>
<%= javascript_include_tag 'rails' %>
<div id="container">
<div id="flash_notice" style="display:none"></div>
<%= content_tag :h1, yield(:title) if show_title? %>
<%= yield %>
</div>
</body>
<div id="post_form"><%= render :partial => 'form' %></div>
<div id="posts_list"><%= render :partial => "posts" %></div>
<div id= "post_errors" style="display:none"></div>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :content %><br />
<%= f.text_area :content, :rows => 5 %>
</p>
<p><%= f.submit %></p>
<% end %>
<tr>
<th>Title</th>
<th>Content</th>
</tr>
<% for post in @posts %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= link_to "Edit", edit_post_path(post), :remote => true %></td>
<td><%= link_to "Destroy", post, :remote => true, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
before_filter :load
def load
@posts = Post.all
@post = Post.new
end
def index
end
def create
@post = Post.new(params[:post])
if @post.save
flash[:notice] = "Successfully created post."
@posts = Post.all
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
flash[:notice] = "Successfully updated post."
@posts = Post.all
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
flash[:notice] = "Successfully destroyed post."
@posts = Post.all
end
end
/*Hide the flash notice div*/
$("#flash_notice").hide(300);
*/Update the html of the div post_errors with the new one*/
$("#post_errors").html("<%= escape_javascript(error_messages_for(@post))%>");
*/Show the div post_errors*/
$("#post_errors").show(300);
<% else -%>
*/Hide the div post_errors*/
$("#post_errors").hide(300);
*/Update the html of the div flash_notice with the new one*/
$("#flash_notice").html("<%= escape_javascript(flash[:notice])%>");
*/Show the flash_notice div*/
$("#flash_notice").show(300);
*/Clear the entire form*/
$(":input:not(input[type=submit])").val("");
*/Replace the html of the div post_lists with the updated new one*/
$("#posts_list").html("<%= escape_javascript( render(:partial => "posts") ) %>");
<% end -%>
$("#flash_notice").hide(300);
$("#post_errors").html("<%= escape_javascript(error_messages_for(@post))%>");
$("#post_errors").show(300);
<% else -%>
$("#post_errors").hide(300);
$("#flash_notice").html("<%= escape_javascript(flash[:notice])%>");
$("#flash_notice").show(300);
$(":input:not(input[type=submit])").val("");
$("#posts_list").html("<%= escape_javascript( render(:partial => "posts") ) %>");
<% end -%>
$("#flash_notice").html("<%= escape_javascript(flash[:notice])%>");
$("#flash_notice").show(300);
$("#posts_list").html("<%= escape_javascript( render(:partial => "posts") ) %>");
And that's it, go to your "/posts" page and you should see our 100% ajax CRUD.
Ps1: That uses html5 to make our forms and links remotes (:remote => true), so it's not gonna work in any version of INTERNET EXPLORER yet.
Ps2: You can also use the redirect/render in your actions pointing to the index and then create only one javascript view (index.js.erb) treating that as you want. I prefer to keep things separated in case I decide to change the visual behavior of some view.
Ps3: Someone should make a generator for this ajax forms.
发表评论
-
7点关于RESTful规范的API接口设计的想法
2016-11-28 14:29 938转:https://segmentfault.co ... -
RESTful API 设计指南
2016-11-28 14:17 401转:http://www.ruanyifeng.com/bl ... -
rails笔记
2016-11-28 13:55 638电子商务系统restful API问卷调查系统考试系统文档 ... -
重构臃肿 ActiveRecord 模型的 7 种方式
2016-11-19 16:29 550转:http://ruby-china.org/topics ... -
Rails系统重构:从单一复杂系统到多个小应用集群
2016-11-17 22:32 432转:http://www.infoq.com/cn/arti ... -
Custom dialog for data-confirm in Rails
2016-10-11 17:24 813Every Rails developers might ... -
常用ruby gem
2016-10-01 12:34 1113常见gems:Devise用于快 ... -
sphinx-0.99 + ultrasphinx
2016-07-27 20:20 470一、Installing Sphinx 1.Extra ... -
rails3 simple captcha
2015-06-03 16:06 629安装: ruby script/plugin insta ... -
测试ruby代码高亮
2015-05-27 16:33 529# encoding: utf-8 require 'd ... -
rails3 time zone
2015-01-04 20:07 502什么是Time Zone,就是时区,UTC或者是GMT ... -
Creating a 100% ajax CRUD using rails 3 and unobtrusive javascript
2014-12-29 22:29 719Creating the project and ... -
rails render用法
2014-12-29 18:55 969render :action => "sho ... -
Rails 3.2 的 Ajax 向导
2014-12-29 18:50 456原文: http://chloerei.com/2012/0 ... -
ror websites
2014-03-04 11:29 525http://railscasts.com/ ht ... -
OAuth gem for rails3
2012-05-21 23:24 790OAuth gem for rails,支持豆瓣,新浪微 ... -
识别验证码
2011-11-18 17:20 1102用imagemagick和tesseract-ocr破解简单 ... -
rails env
2011-04-08 19:41 14191.apt-get安装ruby: ~$ sudo apt-g ... -
搭建rails开发环境
2011-04-08 19:35 952http://www.netbeans.org/ NetB ... -
fragment cache
2011-03-30 01:30 8691.hold the current frament in a ...
相关推荐
jquery-rails, 一个 gem,用于自动使用jQuery和 Rails 3 railsjQuery 面向 Rails 如此伟大。这里 gem 提供:jQuery 1.7.2jQuery UI 1.8.18 ( 仅适用于 javascript )jQuery UJS适配器assert_select_j
这是一个示例应用程序,说明如何通过jQuery在Ajax Crud 4中进行操作。 我们利用$ .getJSON和$ .ajax进行AJAX。 我们没有使用不引人注目JavaScript或模板。 应用程序的根发送到静态控制器的index操作。 因此,请...
Rails 4 jquery Ajax 示例 ... 这是一个如何使用ajax进行CRUD操作的小示例。 安装它 git clone :sagarjunnarkar/RailsAjaxCRUD.git cd RailsAjaxCRUD 捆绑安装 配置 config/database.yml 耙数据库:设置 导轨
jquery-fileupload-rails, 用于 Rails的jQuery文件上传集成 Rails 文件上传jQuery-File-Plugin 是一个文件上传插件,由的Tschan 。 jQuery文件上传功能多文件选择。drag&拖放支持。进度栏和jQuery预览图像。 支持...
rails_apps_composer, 一个 gem,为 Rails 启动应用程序创建 Rails 应用程序模板 Rails 应用编辑器 Rails 应用程序编辑器 gem 安装一个 命令行 工具来从"食谱"的Collection 组装 Rails 应用程序。"你可以使用 rails_...
jquery-ui+jquery-ui-rails
jquery-datatables-rails, 用于 Rails的jquery数据表 gem jquery-datatables-rails 这个 gem 为 jQuery DataTables插件提供了方便,以便与 Rails 资产pipleine结合使用。 它提供所有基本的datatable文件,以及一些...
基于Rails和jQuery的Web应用程序敏捷开发.pdf
从事RAILS开发的人员如何使用AJAX. Learn to build dynamic, interactive web applications using the two most important approaches to web development today: Ajax and the phenomenally efficient Ruby on ...
Ajax on Rails 的 PPT。 pdf格式。
NULL 博文链接:https://michael-roshen.iteye.com/blog/1743358
自述 这篇文章和教程提供了如何使用 Ruby/Rails 构建基本的创建/读取/更新/删除 (CRUD) 应用程序的背景知识。 这篇文章的重点是如何设置文件附件系统并将其投入生产,使用来自 Thoughtbot、Heroku 和 Amazon 为 ...
ember_data_example, 使用 ember.js 和 ember data.js 测试CRUD的简单 Rails 应用 Ember数据示例这个简单的Rails 3.2应用程序创建为演示 Ember.js,ember数据和活动模型序列化程序。 它使用Ember和Ember数据的边缘...
ajax on rails压缩包,全英文
和前两版一样,我们还是先带领读者一步步构造一个真实的应用程序,然后深入介绍Rails内建的各种特性;和前两版不同,我们在第3版中重点介绍了Rails对“资源”的支持,并推荐了当今Rails开发者们的最佳实践。 在《Web...
使用Rails和React创建一个简单的CRUD应用 这是代码存储库,其中包含有关如何创建Rails API的教程,然后使用Webpacker gem来构建React前端以使用它。 教程网址: : 要求 本教程开始时有安装Ruby和Node的说明。 ...
rails-behaviors, jQuery和Zepto的Rails UJS行为 Rails 行为( 不推荐使用)Rails 行为实现了 Rails 3. x. 生成的data-* 行为这是一个替代 jquery-ujs的方法。 首先,它都是CoffeeScript好的,这对我来说很好 ! 其次...
该宝石提供: jQuery 1、2和3 jQuery UJS适配器assert_select_jquery在Ruby测试中测试jQuery响应版本号从v2.1开始,jquery-rails gem遵循以下版本指南,以从Gemfile中更好地控制应用程序的jQuery版本: patch ...
这个gem为Rails打包了jQuery UI资产(JavaScript,样式表和图像),因此您不必再通过下载自定义软件包。 请参阅以查看哪些版本的jquery-ui-rails捆绑了哪些版本的jQuery UI。 警告:此gem与3.0.0之前的jquery-...