AJAX in Ruby-on-Rails. Ruby on Rails and AJAX AJAX can be done with just Javascript Easier if you use libraries –Prototype –SAJAX –jQuery Libraries only.

Slides:



Advertisements
Similar presentations
JavaScript – Quiz #8 Lecture Code:
Advertisements

JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 16 Introduction to Ajax.
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
JavaScript and AJAX Jonathan Foss University of Warwick
JavaScript and Ajax INFO 2310: Topics in Web Design and Programming.
15-Jun-15 Rails and Ajax. HTML Forms The... tag encloses form elements (and usually includes other HTML as well) The arguments to form tell what to do.
Cloud Computing Lecture #7 Introduction to Ajax Jimmy Lin The iSchool University of Maryland Wednesday, October 15, 2008 This work is licensed under a.
Inline, Internal, and External FIle
Fundamentals, DOM, Events, AJAX, UI Doncho Minkov Telerik Corporation
Chapter 6 DOJO TOOLKITS. Objectives Discuss XML DOM Discuss JSON Discuss Ajax Response in XML, HTML, JSON, and Other Data Type.
Prof. James A. Landay University of Washington Spring 2008 Web Interface Design, Prototyping, and Implementation Rich Internet Applications: AJAX, Server.
JQuery CS 268. What is jQuery? From their web site:
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
Javascript and the Web Whys and Hows of Javascript.
NextGen Technology upgrade – Synerizip - Sandeep Kamble.
Christopher Paolini Computational Science Research Center College of Engineering San Diego State University Computational Science 670 Fall 2009 Monday.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
JavaScript & jQuery the missing manual Chapter 11
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
Ajax and Ruby on Rails Session 9 INFM 603.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
ASP.NET Controls. Slide 2 Lecture Overview Identify the types of controls supported by ASP.NET and the differences between them.
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 16 Introduction to Ajax.
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
Lecture 9: AJAX, Javascript review..  AJAX  Synchronous vs. asynchronous browsing.  Refreshing only “part of a page” from a URL.  Frameworks: Prototype,
Developing AJAX Web applications with Castle Monorail David De Florinier Gojko Adzic Skills Matter 12/06/08.
Department Name (View Master > Edit Slide 1) JQuery I took these slides from the site because they were pretty good looking.
Things like Textboxes, Lables, ‘n’at. ASPX page is not HTML Controls are rendered into markup that a browser can understand Some controls are rendered.
AJAX Compiled from “AJAX Programming” [Sang Shin] (Asynchronous JavaScript and XML)
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
Fundamentals, Ajax, Templates, UI Doncho Minkov Telerik Corporation
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
Web Design: Basic to Advanced Techniques Fall 2010 Mondays 7-9pm 200 Sutardja-Dai Hall Introduction to PHP.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
 Registry itself is easy and straightforward in implementation  The objects of registry are actually complicated to store and manage  Objects of Registry.
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Scripting Languages Client Side and Server Side. Examples of client side/server side Examples of client-side side include: JavaScript Jquery (uses a JavaScript.
JQuery and AJAX WEB Technologies : PHP Programming Language.
IS2802 Introduction to Multimedia Applications for Business Lecture 07: Introduction to jQuery Rob Gleasure
AJAX. Overview of Ajax Ajax is not an API or a programming language Ajax aims to provide more responsive web applications In normal request/response HTTP.
Event Handling & AJAX IT210 Web Systems. Question How do we enable users to dynamically interact with a website? Answer: Use mouse and keyboard to trigger.
AJAX and REST. Slide 2 What is AJAX? It’s an acronym for Asynchronous JavaScript and XML Although requests need not be asynchronous It’s not really a.
Understanding JavaScript and Coding Essentials Lesson 8.
05 | Integrating JavaScript and MVC 4 Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek.
COSC 2328 – Web Programming.  PHP is a server scripting language  It’s widely-used and free  It’s an alternative to Microsoft’s ASP and Ruby  PHP.
AJAX CS456 Fall Examples Where is AJAX used? Why do we care?
CS 160 and CMPE/SE 131 Software Engineering February 11 Class Meeting Department of Computer Science Department of Computer Engineering San José State.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
AJAX. Objectives Understand and apply AJAX Using AJAX in DOJO library.
JQuery.
Web API - Introduction AJAX, Spring Data REST SoftUni Team Web API
Not a Language but a series of techniques
12/04/12 JQuery I took these slides from the site because they were pretty good looking. Instructions for editing school and department titles: Select.
Jim Fawcett CSE686 – Internet Programming Spring 2012
AJAX and REST.
AJAX.
AJAX.
Ajax on Rails 28-Nov-18.
DR. JOHN ABRAHAM PROFESSOR UTPA
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Web Programming and Design
Presentation transcript:

AJAX in Ruby-on-Rails

Ruby on Rails and AJAX AJAX can be done with just Javascript Easier if you use libraries –Prototype –SAJAX –jQuery Libraries only handle the client side Ruby on Rails also makes it easy on the server side

Simple Consistent Model A trigger action occurs –button or link, javascript event Data is sent asynchronously Server side action handler takes some action and returns data –may be HTML partial Client side receives the data/HTML and updates the view

Client Side Based on the prototype library Simple Ruby functions –link_to_remote –form_remote_tag –periodically_call_remote –submit_to_remote –remote_function View uses these functions which generate appropriate HTML tags and javascript function calls

View Ajax Demo What time is it? I don't have the time, but <%= link_to_remote( "click here", :update => "time_div", :url =>{ :action => :say_when }) %> and I will look it up.

Details javascript_include_tag –includes the appropriate javascript libraries –:defaults includes a few may also just include specific libraries link_to_remote (can be complex) –most often need text for link name id of the DOM element to update url –may also specify :method - default is POST :position - must be one of :before, :top, :bottom, :after :complete, :success, :failure –javascript callback functions

Server Side AJAX calls are treated just like any other HTTP request However, we don’t want full response layout, css, etc. To suppress full response use –render(:text => “string”) may also use render_text –render(:layout => false) –.js.rjs file with partial –controller with no layout

Example class DemoController < ApplicationController def index end def say_when render_text " The time is " + DateTime.now.to_s + " ” end In this case, there is no view connected to the action method

form_remote_tag { :action => :add_to_cart, :id => product } do -%>

More complex {:controller => "Ajax", :action => "logout" }, :update => 'loginmessage', :complete => remote_function( :url => {:controller => "Ajax", :action => "menu"}, :update => 'sidemenu', :complete => remote_function( :url => {:controller => "Main", :action => "index"} )

.js.rjs templates Controller action methods are paired by name with view files –in app/views/controller_name directory Typically,.html.erb files May also be.js.rjs –ruby javascript –javascript to be executed on load –allows for multiple page updates –written in Ruby Paired with controller action method that doesn’t contain calls to render

.js.rjs example <%= link_to_remote("Test out RJS!", :url =>{ :controller => "shop", :action => "hello" }) %> def hello end page.alert "Hello from RJS!" view code controller code hello.js.rjs - goes into app/views/shop

Updating the DOM with.js.rjs Mark the location to be updated – or Now replace the stuff in that location using page.replace_html –page.replace_html “someid”, “ HI ”.js.rjs file is interpreted into javascript and the javascript is executed when it is loaded

Other page methods alert(message) replace_html(id, *options_for_render) –options for render may be string with html insert_html(position, id, *options_for_render) –position must be :before, :after, :top, :bottom show( *ids ), hide( *ids ), & toggle( *ids ) –toggles the visibility of the html elements visual_effect(effectname, id, options) –page.visual_effect(:fade, ‘someid’, :duration => 0.5) –page.visual_effect(:appear, ‘someid’, :duration => 0.5) –page.visual_effect(:highlight, ‘someid’, :duration => 1)

.js.rjs Much of what you can do is based on the scriptaculous library – Also look at the Ruby on Rails api –ActionView::Helpers::ScriptaculousHelper –ActionView::Helpers::PrototypeHelper

Another Example - View Dynamically populate form elements "sel_con",:url => { :action => :create_select }, :position => "top", :success => "$('sel_con').innerHTML=''" ) do -%> Please select a sports category: <%= select_tag "categories", " Team Individual " %>

Another Example cont - controller def create_select indArr=["Nordic Skiing", "Inline Skating","Tennis", "Triathlon","Road Racing","Figure Skating", "Weight Lifting","Speed Skating","Snowboarding"]; teamArr=["Soccer","Basketball","Football","Hockey", "Baseball","Lacrosse"]; str=""; if (params[:categories].index('Team') != nil) render :partial => "options", :locals => { :sports => teamArr,:sptype => "team"} elsif (params[:categories].index('Individual') != nil) render :partial => "options", :locals => { :sports => indArr, :sptype => "individual" } else str=" unknown "; render :text => str; end

Another Example cont - partial _options " name=" ">