Agile Web Development with Rails, Chapter 11: Task F: Add a Dash of Ajax



Similar documents
Agile Web Development with Rails 4

Deep in the CRUD LEVEL 1

Guides.rubyonrails.org

RESTful Rails Development. translated by Florian Görsdorf and Ed Ruder

Rails 4 Quickly. Bala Paranj.

Web services. This chapter covers

Making OData requests from jquery and/or the Lianja HTML5 Client in a Web App is extremely straightforward and simple.

Learning Web App Development

Building Dynamic Web 2.0 Websites with Ruby on Rails

We automatically generate the HTML for this as seen below. Provide the above components for the teaser.txt file.

Evaluation. Chapter 1: An Overview Of Ruby Rails. Copy. 6) Static Pages Within a Rails Application

HAML und SASS. (und COMPASS) markup haiku vs. syntactically awesome stylesheets. Tobias Adam, Jan Krutisch mindmatters GmbH & Co.

How to create database in GlycomcsPortal?

Intruduction to Groovy & Grails programming languages beyond Java

Online shopping store

Mobile Web Design with HTML5, CSS3, JavaScript and JQuery Mobile Training BSP-2256 Length: 5 days Price: $ 2,895.00

Modern Web Application Framework Python, SQL Alchemy, Jinja2 & Flask

Outline. Lecture 18: Ruby on Rails MVC. Introduction to Rails

Frequently Asked Questions (FAQ)

C:\Users\Micha\Documents\drupal 7 module\improved_multi_select-7.x-2.x-dev.tar\improved_multi_select-7.x-2.x-dev\improved_multi_sel

Developers Guide version 1.1

Ruby On Rails A Cheatsheet. Ruby On Rails Commands

CIS 467/602-01: Data Visualization

Web Authoring CSS. Module Descriptor

1. Tutorial - Developing websites with Kentico Using the Kentico interface Managing content - The basics

Quick Setup Guide. HTML Signatures. A short guide on how to set up HTML Signatures on LetMC s. Last updated 22/11/2012

HTML and CSS. Elliot Davies. April 10th,

Connecting to an ASU computer with Remote Desktop

Looking Good! Troubleshooting Display Problems

How to Re-Direct Mobile Visitors to Your Library s Mobile App

Why API? Using the REST API in an education environment. JAMF Software, LLC

Web Design for Programmers. Brian Hogan NAPCS Slides and content 2008 Brian P. Hogan Do not reproduce in any form without permission

Wicket Application Development

Modern Web Development From Angle Brackets to Web Sockets

Application layer Web 2.0

TECHNICAL WHITEPAPER FOR ADVERTISERS AD MEDIA GUIDE

A RESTful Web Service Interface to the ATLAS COOL Database

HELPDESK SYSTEM (HDS) USER MANUAL

WWW. World Wide Web Aka The Internet. dr. C. P. J. Koymans. Informatics Institute Universiteit van Amsterdam. November 30, 2007

SharePoint Wiki Redirect Installation Instruction

Fast track to HTML & CSS 101 (Web Design)

jquery Sliding Image Gallery

Ruby On Rails. CSCI 5449 Submitted by: Bhaskar Vaish

<?xml version= 1.0?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Generating Open For Business Reports with the BIRT RCP Designer

UComment. UComment is a comment component for Umbraco, it makes it very easy to add comment functionality to any Umbraco content document you wish.

Ready, Set, Go Getting started with Tuscany

NetFlow for SouthWare NetLink

Comparing Dynamic and Static Language Approaches to Web Frameworks

web frameworks design comparison draft - please help me improve it focus on Model-View-Controller frameworks

Selenium An Effective Weapon In The Open Source Armory

Web2CRM Honeypot Captcha Manual

A70 How to Deploy Applications

Commerce Services Documentation

Responsive Web Design Creative License

A Model of the Operation of The Model-View- Controller Pattern in a Rails-Based Web Server

EFORMS MANUAL FOR SHAREPOINT ONLINE

Stylesheet or in-line CSS CSS attributes, used in stylesheets or in-line, can define:

WEEPAY V2 INTEGRATION DOCUMENT (BASIC INTEGRATION)

Slides from INF3331 lectures - web programming in Python

Z-Way Home Automation User Interface Documentation. (c) Z-Wave.Me Team, based on Version 2.0

Example. Represent this as XML

Differences between HTML and HTML 5

GTPayment Merchant Integration Manual

Beginning Web Development with Node.js

MASTERTAG VALUE-ADDED SERVICES FOR ADVERTISERS SET-UP GUIDE

If you, God forbid, find a bug, let me know and I ll try to fix it as soon as I can, ok?

Rich-Internet Anwendungen auf Basis von ColdFusion und Ajax

Enterprise Recipes with Ruby and Rails

Accelerate Your Rails Site with Automatic Generation-Based Action Caching. Rod Cope, CTO and Founder OpenLogic, Inc.

Subject Tool Remarks What is JQuery. Slide Javascript Library

Web Design Basics. Cindy Royal, Ph.D. Associate Professor Texas State University

UNIVERSITY OF CALGARY Information Technologies WEBFORMS DRUPAL 7 WEB CONTENT MANAGEMENT

Shipbeat Magento Module. Installation and user guide

The purpose of jquery is to make it much easier to use JavaScript on your website.

Stripe Event Trigger

MicroStrategy Quick Guide: Creating Prompts ITU Data Mart Support Group, Reporting Services

SharePoint 2007 & 2010 (and Office 365!) Customization for Site Owners End User / Dev (100/200)

M i m o s a A r c h i v e S o f t w a r e ~ S e a r c h i n g f o r m e s s a g e s. Outline. Introduction to Mimosa Archive

How do I share a file with a friend or trusted associate?

How to Build a Model Layout Using Rails

Introduction to Web Technologies

USER GUIDE - May 2010

Transcription:

Agile Web Development with Rails, Chapter 11: Task F: Add a Dash of Ajax David Grayson Based on the book by Sam Ruby. Las Vegas Ruby Meetup, 2012-01-07

This was a long chapter! 30 25 24 20 15 10 8 16 16 14 10 14 Pages 5 0 Ch 5 Ch 6 Ch 7 Ch 8 Ch 9 Ch 10 Ch 11

Iteration F1: Moving the Cart Our goal:

Iteration F1: Moving the Cart app/views/carts/show.html.erb: <%= rer @cart %> app/views/layouts/application.html.erb: <!DOCTYPE html> <html>... <body class="<%= controller.controller_name %>"> <div id="banner">...</div> </div> <div id="columns"> <div id="side"> <div id="cart"><%= rer @cart %></div>... </div> <div id="main"><%= yield %></div> </div> </body></html>

Iteration F1: Moving the Cart "rer @cart" rers app/views/carts/_cart.html.erb: <div class="cart_title">your Cart</div> <table> <%= rer cart.line_items %> <tr class="total_line"> <td colspan="2">total</td> <td class="total_cell"> <%= number_to_currency cart.total_price %> </td> </tr> </table> <%= button_to 'Empty cart', cart, method: :delete, confirm: 'Are you sure?' %>

Iteration F1: Moving the Cart Also need to set @cart in the StoreController#Index: class StoreController < ApplicationController def index @products = Product.order(:title) @cart = current_cart Also need to adjust the stylesheet so the cart looks good in both contexts.

Iteration F1: Moving the Cart app/controllers/line_items_controller.rb: def create @cart = current_cart product = Product.find(params[:product_id]) @line_item = @cart.add_product(product.id) respond_to do format if @line_item.save format.html { redirect_to store_url } format.json { rer json: @line_item, status: :created, location: @line_item } else format.html { rer action: "new" } format.json { rer json: @line_item.errors, status: :unprocessable_entity }

Iteration F1: Moving the Cart Done with Iteration F1! k

Iteration F2: Creating an Ajax cart Ajaxify the button simply by saying remote:true in app/views/store/index.html.erb: <h1>your Pragmatic Catalog</h1> <% @products.each do product %> <div class="entry"> <%= image_tag(product.image_url) %> <h3><%= product.title %></h3> <%= sanitize(product.description) %> <div class="price_line"> <span class="price"><%= number_to_currency(product.price) %></span> <%= button_to 'Add to Cart', line_items_path(product_id: product), remote: true %> </div> </div> <% %>

Iteration F2: Creating an Ajax Cart Prevent the create action from redirecting in line_items_controller.rb: def create @cart = current_cart product = Product.find(params[:product_id]) @line_item = @cart.add_product(product.id) respond_to do format if @line_item.save format.html { redirect_to store_url } format.js format.json { rer json: @line_item, status: :created, location: @line_item } else format.html { rer action: "new" } format.json { rer json: @line_item.errors, status: :unprocessable_entity }

Iteration F2: Creating an Ajax Cart Instead of redirecting, Rails rers app/views/line_items/create.js.erb: $('#cart').html("<%= j rer @cart %>"); L

Iteration F3: Highlighting Changes First, assign an instance variable in line_items_controller.rb: def create @cart = current_cart product = Product.find(params[:product_id]) @line_item = @cart.add_product(product.id) respond_to do format if @line_item.save format.html { redirect_to store_url } format.js { @current_item = @line_item } format.json { rer json: @line_item, status: :created, location: @line_item } else format.html { rer action: "new" } format.json { rer json: @line_item.errors, status: :unprocessable_entity }

Iteration F3: Highlighting Changes Modify app/views/line_items/_line_item.html.erb to use it: <% if line_item == @current_item %> <tr id="current_item"> <% else %> <tr> <% %> <td><%= line_item.quantity %> </td> <td><%= line_item.product.title %></td> <td class="item_price"> <%= number_to_currency(line_item.total_price) %> </td> </tr>

Iteration F3: Highlighting Changes Modify app/views/line_items/create.js.erb to highlight the new item: $('#cart').html("<%=j rer @cart %>"); $('#current_item'). css({'background-color':'#8f8'}). animate({'background-color':'#141'}, 1000); m

Iteration F4: Hiding an Empty Cart app/views/layouts/application.html.erb: <div id="cart" <% if @cart.line_items.empty? %> style="display: none" <% %> > <%= rer(@cart) %> </div> <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %> <%= rer @cart %> <% %> #cart{:style=>('display:none;' if @cart.line_items.empty?)} = rer @cart

Iteration F4: Hiding an Empty Cart Also need to get rid of the "Your cart is currently empty." notice we generated when emptying the cart.

Iteration F4: Hiding an Empty Cart app/views/line_items/create.js.erb: if ($('#cart tr').length == 1) $('#cart').show('blind', 1000); $('#cart').html("<%=j rer @cart %>"); $('#current_item'). css({'background-color':'#8f8'}). animate({'background-color':'#141'}, 1000); N

Iteration F5: Making Images Clickable app/assets/javascripts/store.js.coffee: $ -> $('.store.entry > img').click -> $(this).parent().find(':submit').click()

Testing Ajax Changes First, fix the tests that were breaking. Next, test the new Ajax stuff we added.

Testing Ajax Changes test/functional/line_items_controller_test.rb: test "should create line_item via ajax" do assert_difference('lineitem.count') do xhr :post, :create, product_id: products(:ruby).id assert_response :success assert_select_jquery :html, '#cart' do assert_select 'tr#current_item td', /Programming Ruby 1.9/ $('#cart').html("<%=j rer @cart %>"); app/views/line_items/create.js.erb

Testing Ajax Changes test/functional/store_controller_test.rb: test "markup needed for store.js.coffee is in place" do get :index assert_select '.store.entry > img', 3 assert_select '.entry input[type=submit]', 3

The End