Chapter 15 © 2009 by Addison Wesley Longman, Inc. 1 15.1 Overview of Rails - Rails is a development framework for Web-based applications - Rails is written.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 16 Introduction to Ajax.
HTML Forms. collect information for passing to server- side processes built up from standard widgets –text-input, radio buttons, check boxes, option lists,
Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Ruby on Rails Model of MVC. Model-View-Controller Paradigm A way of organizing a software system Benefits: Isolation of business logic from the user interface.
1.  Understanding about How to Working with Server Side Scripting using PHP Framework (CodeIgniter) 2.
Chapter 15 © 2010 by Addison Wesley Longman, Inc Origins and Uses of Ruby - Designed by Yukihiro Matsumoto; released in Use spread rapidly.
Creating rails app. p:\InstantRails\rails_apps>rails -d mysql cars create create app/controllers create app/helpers create app/models create app/views/layouts.
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.
Presented by Mina Haratiannezhadi 1.  publishing, editing and modifying content  maintenance  central interface  manage workflows 2.
Ruby on Rails Creating a Rails Application Carol E Wolf CS396X.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa.
Ruby on Rails. What is Ruby on Rails? Ruby on Rails is an open source full-stack web framework. It is an alternative to PHP/MySQL. It can render templates,
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
Rails and Grails. To get started Make sure you have java installed You can get the sdk and jre at:
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
CSS Class 6 Make image into a button Create button with button element Group related form elements Control tab order Process form data.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers.
UC Berkeley Hello Rails. Review: MVC Goal: separate organization of data (model) from UI & presentation (view) by introducing controller –mediates user.
JavaScript & jQuery the missing manual Chapter 11
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
Server-side Scripting Powering the webs favourite services.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Ajax and Ruby on Rails Session 9 INFM 603.
FUNCTIONS AND STORED PROCEDURES & FUNCTIONS AND PROTECTING A DB AND PHP (Chapters 9, 15, 18)
1 Dr Alexiei Dingli Web Science Stream Introducing Rails.
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 16 Introduction to Ajax.
COM531 Multimedia Technologies
Ruby on Rails CSE 190M, Spring 2009 Week 6. Overview How to use a database Demo creating a blog application on Rails Explain how the application works.
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.
Associations INFO 2310: Topics in Web Design and Programming.
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
Chapter 15 © 2013 by Pearson Overview of Rails - Rails is a development framework for Web-based applications - Based on MVC architecture for applications.
Forms Collecting Data CSS Class 5. Forms Create a form Add text box Add labels Add check boxes and radio buttons Build a drop-down list Group drop-down.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.
HTML Forms.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
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.
Chapter 12© copyright Janson Industries Java Server Faces ▮ Explain the JSF framework ▮ SDO (service data objects) ▮ Facelets ▮ Pagecode classes.
Chapter 27 Getting “Web-ified” (Web Applications) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Rails and routing INFO 2310: Topics in Web Design and Programming.
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.
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Chapter 28. Copyright 2003, Paradigm Publishing Inc. CHAPTER 28 BACKNEXTEND 28-2 LINKS TO OBJECTIVES Table Calculations Table Properties Fields in a Table.
CS 160 and CMPE/SE 131 Software Engineering February 11 Class Meeting Department of Computer Science Department of Computer Engineering San José State.
Ruby on Rails. Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost.
INTERNET APPLICATIONS CPIT405 Forms, Internal links, meta tags, search engine friendly websites.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
Teaching slides Chapter 6. Chapter 6 Software user interface design & construction Contents Introduction Graphical user interface – Rich window based.
Introduction to RoR Ruby on Rails
Introduction to RoR Ruby on Rails
Play Framework: Introduction
PHP / MySQL Introduction
Chapter 15 Introduction to Rails.
JavaScript is a scripting language designed for Web pages by Netscape.
Presentation transcript:

Chapter 15 © 2009 by Addison Wesley Longman, Inc Overview of Rails - Rails is a development framework for Web-based applications - Rails is written in Ruby and uses Ruby for its applications - Ruby on Rails (RoR) - Based on MVC architecture for applications - MVC cleanly separates applications into three parts: - Model – the data and any restraints on it - View – prepares and presents results to the user - Controller – controls the application - One characterizing part of Rails is its approach to connecting object-oriented software with a relational database – ORM - Maps tables to classes, rows to objects, and columns to fields of the objects

Chapter 15 © 2009 by Addison Wesley Longman, Inc Overview of Rails (continued) - View documents are XHTML documents that may include Ruby code - Most of the controller code is provided by Rails - A Rails application is a program that provides a response when a client browser connects to a Rails-driven Web site - Rails can be used with Ajax - Two fundamental principles that guided the development of Rails: 1. DRY 2. Convention over configuration - Rails does not use a GUI - One simple way to get started with Rails is to download a complete development system - Includes Ruby, Rails, MySQL, Apache and everything else that is necessary

Chapter 15 © 2009 by Addison Wesley Longman, Inc Document Requests - InstantRails is self-contained – not run through a Windows command window - InstantRails is usually installed in a user-defined subdirectory - Static Documents - To use Rails: 1. Click the red I in the directory where Rails was installed 2. Click the black I in the upper left part of the resulting window 3. Select Rails Application to open an other menu 4. Select Open Ruby Console Window - Opens a command-line window - After creating a directory in rails_apps for all related Rails applications, use the rails command in that directory to create a specific application (in our example, greet ) >rails greet - This creates the framework for the new application (more than 45 files in 30 directories) - The app subdirectory has four subdirectories, models, views, controllers, and helpers

Chapter 15 © 2009 by Addison Wesley Longman, Inc Document Requests (continued) - Static Documents (continued) - The generate script can be used to create part of the controller for the application >ruby script/generate controller say - The created controller will have the name say - There are now two files with empty classes in the controller directory, application.rb, and say_controller.rb,. These contain ApplicationController and SayController class SayController < ApplicationController end - Next, add an empty method to the SayController class def hello end - The URL of the Rails application is

Chapter 15 © 2009 by Addison Wesley Longman, Inc Document Requests (continued) - Static Documents (continued) - Next, build the view file, or template, which must reside in the say subdirectory of the views directory of the application and be named hello.html.erb - This is an XHTML document with the following in its body element Hello from Rails! - To test the application, a Web server must be started >ruby script/server - This starts the default server, named Mongrel - Now, pointing the browser to the application produces the displayed view template content

Chapter 15 © 2009 by Addison Wesley Longman, Inc Document Requests (continued) - Static Documents (continued) - Response activities: 1. Instantiate SayController class 2. Call the hello action method 3. Search the views / say directory for hello.html.erb 4. Process hello.html.erb with Erb 5. Return the resulting hello.html.erb to the requesting browser

Chapter 15 © 2009 by Addison Wesley Longman, Inc Document Requests (continued) - Dynamic Documents - There are three ways in Rails to build dynamic documents, but we discuss only one: Embed Ruby code in the template document - An example: display a greeting and the current date and time and the number of seconds since midnight - Ruby code is embedded in a document by placing it between - To insert the result of evaluating the code into the document, use <%= - The Time class has a method, now, that returns the current day of the week, month, day of the month, time, time zone, and year, as a string It is now Number of seconds since midnight:

Chapter 15 © 2009 by Addison Wesley Longman, Inc Document Requests (continued) - Dynamic Documents (continued) - It would be better to put the code in the controller def = * 3600 * 60 end - Now the Ruby code in the template is: It is now Number of seconds since midnight:

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases - We will use MySQL - Use a simple database with just one table - The application will be named cars - The application will present a welcome document to the user, including the number of cars in the database and a form to get the beginning and ending years and a body style for the desired car - Creating the application - In the subdirectory of our examples: >rails –d mysql cars - It is customary to used three databases in a Rails database application: one for development, one for testing, and one for production - To create the three (empty) databases: >rake db:create:all - Creates the database.yml file in the config subdirectory of the application directory ( cars ) - See next page

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) # MySQL. Versions 4.1 and 5.0 are recommended. # development: adapter: mysql encoding: utf8 database: cars_development username: root password: host: localhost test: adapter: mysql encoding: utf8 database: cars_test username: root password: host: localhost production: adapter: mysql encoding: utf8 database: cars_production username: root password: host: localhost

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Build the model, migration script, database table, and maintenance controller for the database >ruby script/generate scaffold Corvette body_style:string miles:float year:integer - The migration class built by this in cars/db/migrate is: class CreateCorvettes < ActiveRecord::Migration def self.up create_table :corvettes do |t| t.string :body_style t.float :miles t.integer :year t.timestamps end def self.down drop_table :corvettes end - We do not have a database—only the description (a migration class) of a database - To create the database, use rake again: >rake db:migrate

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - This second rake command, which executes the self.up method of CreateCorvettes class in 001_create_corvettes.rb, produces the following response: (in C:/myrails/rails_apps/examples/cars) == 1 CreateCorvettes: migrating============== -> s == 2 CreateCorvettes: migrated (0.3440s) ==== - The scema for the new database, schema.rb, which is in cars/db, is: ActiveRecord::Schema.define(:version => 1) do create_table "corvettes", :force => true do |t| t.string "body_style" t.float "miles" t.integer "year" t.datetime "created_at" t.datetime "updated_at“ end - The controller for the application is named corvettes, so we can see the application at

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - If we click New corvette, we get:

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - If we fill out the form, as in: - Now we click Create, which produces:

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Now if we click Back, we get: - If we click Edit, we get:

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - If we click Destroy, we get: - The model file, which is in cars/models, has the empty class: class Corvette < ActiveRecord::Base end - We can easily add some validation to this class validate_presence_of :body_style, :miles, :year

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - The controller built by Rails, named CorvetteController, provides the action methods: index – creates a list of rows of the table show – creates the data for one row new – creates a new row object edit – handles editing a row create – handles row creation update – handles updating a row delete – handles row deletion - There are four documents in the views directory, index.html.erb, new.html.erb, show.html.erb, and edit.html.erb

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) Listing corvettes Body style Miles Year State <%= link_to 'Edit', edit_corvette_path(corvette) %> <%= link_to 'Destroy', corvette, :confirm => 'Are you sure?', :method => :delete %>

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Notice that index.html.erb is only the content of the body element - The rest of the document comes from a layout document, which is stored in the layout subdirectory of views – built by scaffold <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " xhtml1-transitional.dtd"> <html xmlns=" xml:lang="en" lang="en"> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> Corvettes:

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - The new.html.erb document is: New corvette Body style Miles Year State

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - The show.html.erb document is: Body style: Miles: Year: State: <%= link_to 'Edit', %>

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - The edit.html.erb document is: Editing corvette Body style Miles Year State |

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Now we must build the actual application - Build a second controller for the required processes >ruby script/generate controller main - Add an action method for the welcome screen # main_controller.rb - for the cars application class MainController < ApplicationController # welcome method # fetches the value for the initial view def = Corvette.count end - The count method of the table classes returns the number of rows in the table

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - To implement the searches of the table, use find - The find method searches a table for a specific row mycar = Corvettes.find(8) list_five = (1, 2, 3, 4, 5) first_five = Corvettes.find(list_five) The RecordNotFound exception is thrown if find is asked to do something it cannot do The find method can take more complex parameters sixty_five_conv = find(:all, :conditions => "year = 1965 and body_style = 'convertible'") - To use find with non-literal conditions, a different parameter form is needed some_year_conv = find(:all, :conditions => ["year = ? and body_style = - If the first parameter is :first, it gets only the first row that meets the specified condition

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Next, design the welcome template, which has two tasks: 1. Display text boxes to get the model years and body style of interest from the user 2. Display the number of cars furnished by the welcome action method in the controller <!– welcome.html.erb – initial view for the cars application --> Aidan’s Used Car Lot Welcome to our home document We currently have used Corvettes listed To request information on available cars, please fill out the following form and submit it <!– The form to collect input from the user about their interests --> From year: <input type = "text" size = "4" name = "year1" /> To year: <input type = "text" size = "4" name = "year2" /> Body style: <input type = "text" size = "12" name = "body" />

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Next, design the result action method, which is named result in the welcome template form - Task: get form data and use find to compute the required output data for the result template - The form data is made available by Rails through a hash-like object params - params can be indexed by either keys or symbols If phone is the name of a control, we can = params[:phone] - The result method of the main controller is: # result method - fetches values for the result # template def = = = = Corvette.find(:all, :conditions => ["year >= ? and year <= ? and body_style @body]) end

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Last step: build the result template document - Put the information about cars in a table Cars from to with the body style Body Style Miles Year do |car|

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - A filled out request: - The result document:

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Modifying a database - Rails was designed for agile development, so it makes it easy to change to new versions of databases, and also to revert back to earlier versions - The name of the initial version of the migration for the corvettes table was 001_create_corvettes.rb (it is in db/migrate ) - To change a database table, a new migration file is created and rake is used to update the table - To illustrate a change, we add a state column to the corvettes table - To create the new migration file: >ruby script/generate migration AddStateToCorvette state:string - Produces the response: exists db/migrate create db/migrate/002_add_state_to_corvettes.rb

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Modifying a database (continued) - The resulting migration class, named 002_add_state_to_corvette/rb : class AddStateToCorvette < ActiveRecord:Migration def self.up add_column :corvettes, :state, :string end def self.down remove_column :corvettes, :state end - Now use rake to apply the migration to the table: >rake db:migrate - Rails response: (in c:\myrails\rails_apps\examples\cars) == 1 AddStateToCorvette: migrating ============= -- add_column(:corvettes, :state, :string) -> s == 2 AddStateToCorvette: migrated (0.5160s) ====

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Modifying a database (continued) - The revised schema is: ActiveRecord::Schema.define(:version => 2) do create_table "corvettes", :force => true do |t| t.string "body_style" t.float "miles" t/integer "year" t.datetime "created_at" t.datetime "updated_at" t.string "state" end - Now the display of corvettes is:

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Modifying a database (continued) - To go back to the last migration form: >rake db:rollback - To go back to any migration: >rake db:migrate VERSION=1

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Using MySQL in Rails - Go to the mysql/bin directory and start MySQL >mysql [-h host] [-u username] [database_name] [-p] - To fill a table using MySQL: mysql> use cars_development; - Put the INSERT command into the fill_table.sql file mysql>mysql –u root < fill_table.sql - Layouts - Rails provided one for the corvettes controller - We could build one for the main controller - Include the DOCTYPE, some headings, and a footer for a copyright

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " /xhtml1-transitional.dtd"> <html xmlns=" xml:lang="en" lang="en"> Main Aidan's Used Car Lot Welcome to our home document Copyright 2009, AUCL, Inc. - We could also add a stylesheet for the templates of main - Could be used for both the actual template file and the layout /* mainstyles.css - a style sheet for the main controller */ h1 {font-style: italic; color: blue;} h2 {color: blue;}.labels {font-style: italic; color: red;} - Stored in the cars/public/stylesheets directory

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - To reference the stylesheet in the layout, add the following to the head of the layout document - The form of the main template must be changed to use the styles From year: <input type = "text" size = "4" name = "year1" /> To year: <input type = "text" size = "4" name = "year2" /> Body style: <input type = "text" size = "12" name = "body" /> <input type = "submit" value = "Submit request" /> <input type = "reset" value = "Reset form" />

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails Applications with Databases (continued) - Now the welcome template appears as:

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails with Ajax - There is a library of JavaScript utilities named Prototype, which has much of what is needed - To access it, place the following in the Rails template document: - Problems with using Rails and Ajax - Not all elements can be modified or replaced on all browsers using only the Rails tools - A simple solution: Use div elements for content that is to be replaced using Ajax - Rails implements Ajax as follows: 1. Triggered by a user event or a timer 2. Data associated with the event is sent asynchronously to a controller method (an action handler) on the server using an XHR object 3. The controller action handler performs some operation and returns text, XML or XHTML 4. JavaScript on the browser receives the response and performs the update to the document

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails with Ajax (continued) - Example – popcorn form, again - The application is named popcornA, the controller is named popcorn, and the controller class is named PopcornController - The initial action method (and the view template) are named show_form  SHOW show_form.html.erb - Rails uses several different helper methods to trigger the Ajax process, most commonly link_to_remote and observe_field - These are JavaScript functions wrapped in Ruby methods - observe_field can take many parameters - First parameter - a literal string with the id of the control element to watch - Other parameters appear as the elements of a hash literal - The name of the action method: :url => (:action => process_form)

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails with Ajax (continued) - Example (continued) - More parameters :update - When :update is specified, the controller action given in :url produces the response using the render method - The :text parameter is passed to render :text is set (with => ) to a string value render :text => " … " :with - A JavaScript expression giving the first parameter for the XHR request object e.g., "’zip’=value" :frequency - The frequency in seconds at which changes to the specified widget will be detected - Not setting a frequency causes an event to be used, instead of a timer ( changed for text boxes and click for buttons), rather than time-based observations.

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails with Ajax (continued) - Example (continued) :complete, :success, :failure - To specify the callback function to be called when the Ajax operation is completed - Complete example call to observer_field : <%= observe_field("my_button", :update => "name" :url => {:action => process_form}) %> - Recall that the controller action method for the application gets the response, splits it, and updates the text boxes for city and state - This code is the content of a javascript_tag, because it will be in a view template, which is an html.erb file <%= javascript_tag <<-END function update_city_state(xhr) { result = xhr.responseText.split(', '); document.getElementById('city').value = result[0]; document.getElementById('state').value = result[1]; } END %>

Chapter 15 © 2009 by Addison Wesley Longman, Inc Rails with Ajax (continued) - Example (continued) - Because this application updates two elements, :update is not used; rather, the name of the update function is given with complete <$= observe_field("zip", :url => {:action => :fill_city_state}, :complete => 'update_city_state(request); ', :with => 'zip' ) %> - The Controller - The action method, fill_city_state, generates the city and state for the form, given the zip code as a POST parameter  SHOW popcorn_controller.rb - In a Rails application, the new partial document could be produced by either the controller method or in the template document - If the template does it, the action method of the controller must include render(:layout => false) - If the action method does it, it uses render