- 浏览: 178289 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 974转:https://segmentfault.co ... -
RESTful API 设计指南
2016-11-28 14:17 442转:http://www.ruanyifeng.com/bl ... -
rails笔记
2016-11-28 13:55 675电子商务系统restful API问卷调查系统考试系统文档 ... -
重构臃肿 ActiveRecord 模型的 7 种方式
2016-11-19 16:29 590转:http://ruby-china.org/topics ... -
Rails系统重构:从单一复杂系统到多个小应用集群
2016-11-17 22:32 472转:http://www.infoq.com/cn/arti ... -
Custom dialog for data-confirm in Rails
2016-10-11 17:24 840Every Rails developers might ... -
常用ruby gem
2016-10-01 12:34 1163常见gems:Devise用于快 ... -
sphinx-0.99 + ultrasphinx
2016-07-27 20:20 498一、Installing Sphinx 1.Extra ... -
rails3 simple captcha
2015-06-03 16:06 662安装: ruby script/plugin insta ... -
测试ruby代码高亮
2015-05-27 16:33 560# encoding: utf-8 require 'd ... -
rails3 time zone
2015-01-04 20:07 532什么是Time Zone,就是时区,UTC或者是GMT ... -
Creating a 100% ajax CRUD using rails 3 and unobtrusive javascript
2014-12-29 22:29 756Creating the project and ... -
rails render用法
2014-12-29 18:55 1009render :action => "sho ... -
Rails 3.2 的 Ajax 向导
2014-12-29 18:50 496原文: http://chloerei.com/2012/0 ... -
ror websites
2014-03-04 11:29 595http://railscasts.com/ ht ... -
OAuth gem for rails3
2012-05-21 23:24 816OAuth gem for rails,支持豆瓣,新浪微 ... -
识别验证码
2011-11-18 17:20 1129用imagemagick和tesseract-ocr破解简单 ... -
rails env
2011-04-08 19:41 14421.apt-get安装ruby: ~$ sudo apt-g ... -
搭建rails开发环境
2011-04-08 19:35 986http://www.netbeans.org/ NetB ... -
fragment cache
2011-03-30 01:30 9251.hold the current frament in a ...
相关推荐
这是一个示例应用程序,说明如何通过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 耙数据库:设置 导轨
Ruby on Rails,简称Rails,是由David Heinemeier Hansson基于Ruby语言开发的一个开源Web应用程序框架。Rails遵循MVC(模型-视图-控制器)架构模式,致力于“约定优于配置”和“Don't Repeat Yourself”(DRY)的...
11. **AJAX和JavaScript**:了解如何在Rails中集成异步请求,以及使用CoffeeScript或jQuery增强用户体验。 通过这些源代码,读者可以对照书中内容,逐步实现各个功能模块,遇到问题时可以直接查看代码实例,进行...
2dc_jqgrid, 这个插件允许你很容易地将jQuery添加到你的Rails 应用程序中 2 dcJqgrid这个 Rails 插件允许你将jQuery添加到你的应用程序中。支持以下功能:已经启用 Ajax排序分页搜索CRUD操作( 添加,编辑,删除)多...
书中会讲解如何定义模型、创建和迁移数据库表,以及执行CRUD(Create、Read、Update、Delete)操作。 6. **视图和模板**:了解ERB(Embedded Ruby)模板语言,学习如何创建HTML页面并嵌入Ruby代码,以动态生成内容...
8. **AJAX和JavaScript**:Rails与jQuery等JavaScript库的集成,使得创建动态、交互性强的Web应用变得容易,书中会讲解如何实现无刷新页面更新。 9. **部署与维护**:讲解如何将Rails应用部署到生产环境,以及如何...
9. **AJAX与JavaScript**:Rails与jQuery和CoffeeScript等JavaScript库有良好的集成,书中可能探讨如何创建异步请求和实现动态更新。 10. **部署(Deployment)**:书中可能会介绍如何将Rails应用部署到Heroku、...
此外,本书还涉及到了Ajax(异步JavaScript和XML)在Rails中的应用,如何利用Unobtrusive JavaScript(UJS)实现页面的无刷新更新,提升用户体验。同时,还会介绍如何集成第三方库和插件,如jQuery和Devise,以及...
【标题】"myFirstApp:Ruby on Rails 的第一个应用程序"是一个初学者的教程,旨在引导开发者通过Ruby on Rails框架创建他们的第一个Web应用。这个课程可能是2014年2月时进行的,那时Ruby on Rails的版本可能相对较新...
YellBlog这个项目,正如其名,可能是一个简单的博客系统,用于展示如何在Rails中创建、读取、更新和删除(CRUD)数据。 【描述】"这是第一个Rails演示应用程序。" 这个描述意味着YellBlog是初学者接触Rails的第一...
本次聊天使用: MySQL 和 ActiveRecord Javascript、Jquery 和 ajax 调用Ruby 2.1 导轨 4 css3 html5 陷阱: 此应用程序不使用迁移、数据库种子、复杂的会话、表单助手等。 我们创建它只是为了好玩。 代码库可以...
Ruby on Rails,简称Rails,是由David Heinemeier Hansson创建的一个开源Web应用程序框架,它遵循“Don't Repeat Yourself”(DRY)和“Convention over Configuration”(CoC)的原则,极大地提高了开发效率。...
- **Scaffold**:Rails的`scaffold`命令用于快速生成包括模型、控制器、视图和迁移在内的完整CRUD(创建、读取、更新、删除)功能。 ### 4. **数据库操作** - **数据库迁移**:`db:migrate`命令用于执行数据库迁移...
"beerNotes"是一个基于Ruby on Rails框架开发的CRUD(创建、读取、更新、删除)应用程序,专门用于记录啤酒酿造的过程和相关信息。这个应用程序可能是酿酒爱好者或专业酿酒师用来跟踪酿造配方、工艺细节以及品尝笔记...
【标题】"asada:一个月的Rails应用程序定制的tumblr + ig画廊"指的是一个基于Ruby on Rails框架开发的项目,旨在构建一个类似Tumblr和Instagram画廊的混合平台。这个项目可能融合了社交网络的功能与图片分享的核心...
1. **创建项目**: 使用 `rails new` 命令初始化一个新的 Rails 项目,如 `rails new RailsTutorial_DemoApp`。 2. **数据库迁移**: 创建数据表结构,使用 `rails generate migration` 创建迁移文件,然后执行 `...
安装Rails后,你将学会使用`rails new`命令创建一个新的Rails应用,并理解应用的基本目录结构。 接着,教程会引导你创建数据库模型,使用ActiveRecord,这是Rails内置的ORM(对象关系映射)工具。通过定义模型类,...
Ruby on Rails(简称Rails)是一个基于Ruby语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。它鼓励使用"Convention over Configuration"(约定优于配置)的原则,使得开发过程更加简洁高效。...
在“anime-talk: Rails + JavaScript项目-阶段4”中,我们关注的是使用Ruby on Rails框架与JavaScript技术构建一个动漫讨论平台的第四阶段。这个项目旨在教你如何将后端的强大力量与前端的动态交互性相结合,以创建...