1. in the last chapter, in the user profile screen, the user can click on the follow button, then
he is redirect back to the original profile screen.
so we can ask, why do we leave that profile page at all?
that is a waste of resource.
right, this is exactly the problem solved by Ajax.
Ajax allows send request to the server without leaving the page.
2. because using Ajax in the form is so common, (for example, when submit the form, you want to use Ajax feature.) rails make it very easy!
you just need to add a param to the form_for
:remote => true
the rails wil automatically use Ajax!
3. (Ajax only works if javascript is enabled in the browser, but it is very easy to degrade gracefully. by this way:
in the server side, we detect it is Ajax or normal request, then deal it differently:
respond_to do |format|
format.html { redirect_to @user }
format.js
end
)
4. we already updated the form code to send an Ajax request, then we need to update the code in the controller to deal with Ajax request, first, TDD.
describe RelationshipsController do . . . describe "POST 'create'" do . . . it "should create a relationship using Ajax" do lambda do xhr :post, :create, :relationship => { :followed_id => @followed } response.should be_success end.should change(Relationship, :count).by(1) end end describe "DELETE 'destroy'" do . . . it "should destroy a relationship using Ajax" do lambda do xhr :delete, :destroy, :id => @relationship response.should be_success end.should change(Relationship, :count).by(-1) end end end
5. next, we will do the action code:
class RelationshipsController < ApplicationController before_filter :authenticate def create @user = User.find(params[:relationship][:followed_id]) current_user.follow!(@user) respond_to do |format| format.html { redirect_to @user } format.js end end def destroy @user = Relationship.find(params[:id]).followed current_user.unfollow!(@user) respond_to do |format| format.html { redirect_to @user } format.js end end end
6. next, we need to add two js.erb file to be rendered after the action:
create.js.erb
and
destroy.js.erb
$("follow_form").update("<%= escape_javascript(render('users/unfollow')) %>") $("followers").update('<%= "#{@user.followers.count} followers" %>')
$("follow_form").update("<%= escape_javascript(render('users/follow')) %>")
$("followers").update('<%= "#{@user.followers.count} followers" %>')
7. here we used prototype javascript, in rails 3.1, they default to use jquery.
in jquery, if you want to get a element by its id, you need to use:
$('#follow_form').html("<%= escape_javascript(render('users/unfollow')) %>")
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 816the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8521. we need to get all the micro ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8761.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7471. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8931. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6981. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11031. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 733in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 768in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 657again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7051. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 732whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7411. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 722There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 655start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 643a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 714in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 776Chapter 8.3 this part, we will ...
相关推荐
Delphi 12.2A031139253-26435漂亮的Delphi软键盘 屏幕键盘.rar
《iOS12.2 Developer Disk Image详解》 iOS12.2 Developer Disk Image是苹果公司为开发者提供的一个重要工具,主要用于支持iOS12.2版本的真机编译和调试工作。这一工具对于iOS应用开发者来说至关重要,因为它允许...
标题 "12.2 (16E227)" 指的是苹果iOS操作系统的一个特定版本,即iOS 12.2。这个版本的内部构建号是16E227,通常这样的数字序列用于区分不同阶段的更新或修复。在iOS开发中,每个版本的系统都需要与Xcode的开发者工具...
《K3 WISE 12.2与K/3 V12.2标准版:深度解析及下载指南》 K3 WISE 12.2 和金蝶K/3 V12.2标准版是企业信息化管理的重要工具,这两款软件在企业管理软件市场中占据了重要的地位,尤其适用于中大型企业的财务、供应链...
DevExpress 12.2 完美汉化包是一款针对DevExpress控件库12.2版本的中文语言包。DevExpress是一家知名的软件开发公司,提供了一系列高效、功能丰富的UI控件和开发工具,广泛应用于Windows Forms、ASP.NET、WPF、...
本文将深入探讨“ios12.2真机调试包”相关知识点,包括iOS 12.2的特性、真机调试的重要性以及如何进行真机调试。 首先,iOS 12.2是苹果公司发布的一个操作系统更新,其版本号为16E5191d,通常包含性能优化、新功能...
本文将深入探讨“iOS 12.2真机调试包”这一主题,以及如何利用Xcode进行有效的iOS 12.2真机调试。 首先,我们需要理解iOS 12.2这一操作系统版本。iOS 12.2是苹果公司在2019年发布的一个重要更新,带来了多项新功能...
《iOS 12.2 (16E5191d):Xcode真机测试与Developer Disk Image详解》 在iOS开发过程中,确保设备与开发环境的兼容性至关重要。当我们提到“iOS 12.2 (16E5191d)”时,我们指的是苹果公司发布的iOS操作系统的一个...
PostgreSQL 12.2 安装手册 PostgreSQL 是一种功能强大且广泛使用的开源关系数据库管理系统。随着版本的更新,PostgreSQL 12.2 成为当前主流的数据库管理系统版本之一。本文档旨在指导用户如何在 CentOS 7.7 操作...
《Xilinx ISE 12.2 License详解》 Xilinx ISE(Integrated Software Environment)是Xilinx公司推出的一款综合的、强大的FPGA(Field-Programmable Gate Array)设计工具套件。它为FPGA开发者提供了从概念设计到...
12.2 (16E5181e), 路径: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
### Oracle EBS R12.2 新增功能特性解析 #### 概述 Oracle E-Business Suite (EBS) R12.2 是一个重要的版本更新,它在原有的 R12 版本基础上进行了大量的改进与增强。这些新功能不仅提高了系统的性能与安全性,还...
Xilinx ISE 12.2 版本是该系列软件的一个重要版本,它包含了众多的设计工具,如VHDL和Verilog编译器、约束编辑器、逻辑综合器、适配器、时序分析器以及配置工具等。 在Xilinx ISE 12.2中,license文件是软件运行的...
JM 12.2编码流程图
Xcode 12.2是Xcode的一个特定版本,它包含了iOS 12.2的软件开发工具包(SDK)。SDK(Software Development Kit)是一组工具、库、文档和技术,帮助开发者针对特定操作系统或硬件平台创建应用程序。iOS 12.2 SDK提供...
iOS12.2真机调试包是专为iOS开发人员设计的,用于在运行iOS12.2系统的设备上进行应用程序的测试和调试。这个压缩包文件名为"12.2 (16E5191d)",可能包含了该特定版本的固件更新,以及相关的开发者工具和文档。 在...
oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
《iOS12.2真机运行包:深入解析与应用》 iOS12.2作为苹果公司推出的系统更新,带来了诸多新特性和性能优化,尤其是对于开发者而言,它提供了更强大的开发工具和平台支持。本篇文章将围绕"iOS12.2真机运行包"进行...