`
peryt
  • 浏览: 54359 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

12.2 a working follow button with Ajax

 
阅读更多

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')) %>")

分享到:
评论

相关推荐

    iOS12.2 Developer Disk Image

    《iOS12.2 Developer Disk Image详解》 iOS12.2 Developer Disk Image是苹果公司为开发者提供的一个重要工具,主要用于支持iOS12.2版本的真机编译和调试工作。这一工具对于iOS应用开发者来说至关重要,因为它允许...

    12.2 (16E227)

    标题 "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标准版:深度解析及下载指南》 K3 WISE 12.2 和金蝶K/3 V12.2标准版是企业信息化管理的重要工具,这两款软件在企业管理软件市场中占据了重要的地位,尤其适用于中大型企业的财务、供应链...

    ios12.2真机调试包

    本文将深入探讨“ios12.2真机调试包”相关知识点,包括iOS 12.2的特性、真机调试的重要性以及如何进行真机调试。 首先,iOS 12.2是苹果公司发布的一个操作系统更新,其版本号为16E5191d,通常包含性能优化、新功能...

    iOS 12.2真机调试包

    本文将深入探讨“iOS 12.2真机调试包”这一主题,以及如何利用Xcode进行有效的iOS 12.2真机调试。 首先,我们需要理解iOS 12.2这一操作系统版本。iOS 12.2是苹果公司在2019年发布的一个重要更新,带来了多项新功能...

    ios 12.2 (16E5191d)

    《iOS 12.2 (16E5191d):Xcode真机测试与Developer Disk Image详解》 在iOS开发过程中,确保设备与开发环境的兼容性至关重要。当我们提到“iOS 12.2 (16E5191d)”时,我们指的是苹果公司发布的iOS操作系统的一个...

    devexpress 12.2完美汉化包

    DevExpress 12.2 完美汉化包是一款针对DevExpress控件库12.2版本的中文语言包。DevExpress是一家知名的软件开发公司,提供了一系列高效、功能丰富的UI控件和开发工具,广泛应用于Windows Forms、ASP.NET、WPF、...

    xilinx ISE 12.2 license

    《Xilinx ISE 12.2 License详解》 Xilinx ISE(Integrated Software Environment)是Xilinx公司推出的一款综合的、强大的FPGA(Field-Programmable Gate Array)设计工具套件。它为FPGA开发者提供了从概念设计到...

    iOS12.2 真机调试包

    12.2 (16E5181e), 路径: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport

    Oracle EBS R12.2 新增功能特性文档

    ### Oracle EBS R12.2 新增功能特性解析 #### 概述 Oracle E-Business Suite (EBS) R12.2 是一个重要的版本更新,它在原有的 R12 版本基础上进行了大量的改进与增强。这些新功能不仅提高了系统的性能与安全性,还...

    xilinx ise 12.2 license

    Xilinx ISE 12.2 版本是该系列软件的一个重要版本,它包含了众多的设计工具,如VHDL和Verilog编译器、约束编辑器、逻辑综合器、适配器、时序分析器以及配置工具等。 在Xilinx ISE 12.2中,license文件是软件运行的...

    PostgreSQL 12.2 安装手册

    PostgreSQL 12.2 安装手册 PostgreSQL 是一种功能强大且广泛使用的开源关系数据库管理系统。随着版本的更新,PostgreSQL 12.2 成为当前主流的数据库管理系统版本之一。本文档旨在指导用户如何在 CentOS 7.7 操作...

    JM 12.2编码流程图

    JM 12.2编码流程图

    IOS Xcode 12.2 Sdk

    Xcode 12.2是Xcode的一个特定版本,它包含了iOS 12.2的软件开发工具包(SDK)。SDK(Software Development Kit)是一组工具、库、文档和技术,帮助开发者针对特定操作系统或硬件平台创建应用程序。iOS 12.2 SDK提供...

    iOS12.2真机调试包

    iOS12.2真机调试包是专为iOS开发人员设计的,用于在运行iOS12.2系统的设备上进行应用程序的测试和调试。这个压缩包文件名为"12.2 (16E5191d)",可能包含了该特定版本的固件更新,以及相关的开发者工具和文档。 在...

    iOS12.2 真机运行包

    《iOS12.2真机运行包:深入解析与应用》 iOS12.2作为苹果公司推出的系统更新,带来了诸多新特性和性能优化,尤其是对于开发者而言,它提供了更强大的开发工具和平台支持。本篇文章将围绕"iOS12.2真机运行包"进行...

    Xcode 12.2镜像文件支持真机调试

    标题提到的“Xcode 12.2镜像文件支持真机调试”意味着这个压缩包提供了Xcode 12.2版本的一个特殊构建,它允许开发者在实际设备上调试iOS 12.2(16E5181e)系统版本的应用。 Xcode的真机调试功能对于开发者来说至关...

    Devexpress XAF 12.2 破解文件

    Devexpress XAF 12.2 破解文件

Global site tag (gtag.js) - Google Analytics