Forms (Engineering Software as a Service §4.6) © 2013 Armando Fox & David Patterson, all rights reserved 1.

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

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.
© 2013 Armando Fox & David Patterson, all rights reserved
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.
Edoclite and Managing Client Engagements What is Edoclite? How is it used at IU? Development Process?
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 Guide to JSP common functions 1.Including the libraries as per a Java class, e.g. not having to refer to java.util.Date 2.Accessing & using external.
® IBM Software Group © 2006 IBM Corporation JSF Tab Controls This Learning Module shows how to develop server-side EGL applications with dynamic content.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
RUBY ON RAILS Mark Zhang. In this talk  Overview of Ruby on Rails  Core ideas  Show a tiny bit of example code  Touch on several general web development/
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,
Ruby on Rails CSCI 6314 David Gaspar Jennifer Garcia Avila.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
JavaScript Form Validation
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.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
® IBM Software Group © 2006 IBM Corporation Writing Good Use Cases Module 4: Detailing a Use Case.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Background The Encyclopedio of Life (EOL) is an ROR open source project to create a free, online reference source and database for every one of the 1.8.
Server-side Scripting Powering the webs favourite services.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Chapter 4.1 – Deeper into Rails Maciej Mensfeld Presented by: Maciej Mensfeld Deeper into Rails mensfeld.pl github.com/mensfeld senior.
What is Sure BDCs? BDC stands for Batch Data Communication and is also known as Batch Input. It is a technique for mass input of data into SAP by simulating.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
1 Dr Alexiei Dingli Web Science Stream Advanced ROR.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
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.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch February CSCE 740 Software Engineering.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Peter Laird. | 1 Building Dynamic Google Gadgets in Java Peter Laird Managing Architect WebLogic Portal BEA Systems.
Routes & REST & URL-helpers & validations & filters
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
1 Version /05/2004 © 2004 Robert Oshana Requirements Engineering Use cases.
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
MEMBERSHIP AND IDENTITY Active server pages (ASP.NET) 1 Chapter-4.
1 Presentation Title Test-driven development (TDD) Overview David Wu.
COMP3241 E-Commerce Technologies Richard Henson University of Worcester November 2014.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
PostBack  When an initial request for a page (a Web Form) is received by ASP.NET, it locates and loads the requested Web Form (and if necessary compiles.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
: Information Retrieval อาจารย์ ธีภากรณ์ นฤมาณนลิณี
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
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.
LOGIN FORMS.
We Behatin’. INTRODUCTION Behat background 3 Behat prep Getting started
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -
Distributed Web Systems Cookies and Session Tracking Lecturer Department University.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Section 10.1 Define scripting
JavaScript, Sixth Edition
Forms (Engineering Software as a Service §4.6)
Managing State Chapter 13.
Play Framework: Introduction
CMPE 280 Web UI Design and Development October 24 Class Meeting
Behavioral Driven Development (BDD) (Engineering Software as a Service §7) Philip Ritchey slides generously gifted by Jeff Huang.
Presentation transcript:

Forms (Engineering Software as a Service §4.6) © 2013 Armando Fox & David Patterson, all rights reserved 1

Dealing with Forms Creating a resource usually takes 2 interactions –new: Retrieve blank form –create: Submit filled form How to generate/display? How to get values filled in by user? What to “return” (render)? 2

Rails Cookery #3 To create a new submittable form: 1. Identify the action that serves the form itself 2. Identify the action that receives submission 3. Create routes, actions, views for each Form elements’ name attributes will appear as keys in params[] Helpers provided for many common elements 3

Creating the Form Anatomy of a form in HTML –the action and method attributes (i.e., the route) –only named form inputs will be submitted Generating the form in Rails –often can use URI helper for action, since it’s just the URI part of a route (still need method) –form field helpers (see api.rubyonrails.org) generate conveniently-named form inputsapi.rubyonrails.org 4

5 END

%form{:action => '/movies', :method => 'post'} All of the above = form_tag movies_path do... end ☐ ☐ ☐ ☐ 6 Which of these would be valid for generating the form that, when submitted, would call the Create New Movie action?

7 END

Redirection, the Flash and the Session (Engineering Software as a Service §4.7) © 2013 Armando Fox & David Patterson, all rights reserved 8

Receiving the Form A neat trick: use debugger to inspect what’s going on –start with rails server --debugger –insert debugger where you want to stop –details & command summary: ESaaS §4.7 NOTE: params[:movie] is a hash, because of the way we named form fields –Conveniently, just what Movie.create! wants 9

What View Should Be Rendered for Create Action? Idiom: redirect user to a more useful page –e.g., list of movies, if create is successful –e.g., New Movie form, if unsuccessful Redirect triggers a whole new HTTP request –How to inform user why they were redirected? Solution: flash[] —quacks like a hash that persists until end of next request –flash[:notice] conventionally for information –flash[:warning] conventionally for “errors” 10

Flash & Session session[] : like a hash that persists forever –reset_session nukes the whole thing –session.delete(:some_key), like a hash By default, cookies store entire contents of session & flash –Alternative: store Rails sessions in DB table –(Search “rails session use database table”) –Another alternative: store sessions in a “NoSQL” storage system, like memcached 11

12 END

False, because you can’t put arbitrary objects into a hash False, because session[] isn’t really a hash, it just quacks like one True - knock yourself out! ☐ ☐ ☐ ☐ 13 Ben Bitdiddle says: “You can put arbitrary objects (not just “simple” ones like ints and strings) into the session[].” What do you think?

14 END

Administrivia Get account for “public projects” on PivotalTracker.com, add the TAs and me –Public projects are free Get account on CodeClimate.com –Provides code analysis –Free for an open source (OSS) repo –Sign in with your GitHub account 15

16 END

Finishing CRUD (Engineering Software as a Service §4.8) © 2013 Armando Fox & David Patterson, all rights reserved 17

Edit/Update Pair is Analogous to New/Create Pair What’s the same? –1 st action retrieves form, 2 nd action submits it –“submit” uses redirect (to show action for movie) rather than rendering its own view What’s different? –Form should appear with existing values filled in: retrieve existing Movie first –Form action uses PUT rather than POST

Destroy is Easy Remember, destroy is an instance method –Find the movie first...then destroy it –Send user back to Index def = flash[:notice] = "Movie deleted." redirect_to movies_path end 19

20 END

Only this request - once the view is rendered, the variable is reset to nil It depends on whether the instance variable was declared static This request and all subsequent requests ☐ ☐ ☐ ☐ 21 If you set an instance variable in a controller method, its value will be retained for how long?

22 END

Fallacies, Pitfalls, and Perspectives on SaaS-on-Rails (Engineering Software as a Service § ) © 2013 Armando Fox & David Patterson, all rights reserved 23

Fat Controllers & Fat Views Really easy to fall into “fat controllers” trap –Controller is first place touched in your code –Temptation: start coding in controller method Fat views –“All I need is this for-loop” –“....and this extra code to sort the list of movies differently” –“...and this conditional, in case user is not logged in” No! That’s for model, controller, helpers 24

Designing for Service-Oriented Architecture A benefit of thin controllers & views: easy to retarget your app to SOA Typically, SOA calls will expect XML or JSON (JavaScript Object Notation, looks like nested hashes) instead of HTML A trivial controller change accomplishes this 25

26 END

Only (b) and (c) Only (a) and (c) Only (a) and (b) ☐ ☐ ☐ ☐ 27 Which steps are ALWAYS required when adding a new action 'foo' to the Movie model of a Rails app: (a) Ensure there is a template to render in app/views/movies/foo.html.haml (or.html.erb, etc) (b) Ensure a route exists in config/routes.rb (c) Implement helper method to generate necessary route- helper URIs

28 END

Introducing Cucumber & Capybara (Engineering Software as a Service §7.6) 29 © 2013 Armando Fox & David Patterson, all rights reserved

User Stories => Acceptance Tests? Wouldn’t it be great to automatically map 3x5 card user stories into tests for user to decide if accept the app? How would you match the English text to test code? How could you run the tests without a human in the loop to perform the actions? 30

Cucumber: Big Idea Tests from customer-friendly user stories –Acceptance: ensure satisfied customer –Integration: ensure interfaces between modules consistent assumptions, communicate correctly Cucumber meets halfway between customer and developer –User stories are not code, so clear to customer and can be used to reach agreement –Also not completely freeform, so can connect to real tests 31

Example User Story Feature: User can manually add movie Scenario: Add a movie Given I am on the RottenPotatoes home page When I follow "Add new movie" Then I should be on the Create New Movie page When I fill in "Title" with "Men In Black" And I select "PG-13" from "Rating" And I press "Save Changes" Then I should be on the RottenPotatoes home page And I should see "Men In Black" 32 3 to 8 Steps / Scenario ≥1 Scenarios / Feature 1 Feature

Cucumber User Story, Feature, and Steps User story: refers to single feature Feature: ≥1 scenarios that show different ways a feature is used –Keywords Feature and Scenario identify respective components –Kept in.feature files Scenario: steps that describe scenario Step definitions: Ruby code to test steps –Kept in X_controller.rb files 33

5 Step Keywords 1. Given steps represent state of world before event: preconditions 2. When steps represent event –e.g., simulate user pushing a button 3. Then steps represent expected postconditions; check if true 4./ 5. And & But extend previous step 34

Steps => Step Definitions via Regular Expressions Regexes match English phrases in steps of scenarios to step definitions! Given /^(?:|I )am on (.+)$/ “I am on the Rotten Potatoes home page” Step definitions (Ruby code) likely use captured string –“Rotten Potatoes home page” 35

More on “Cuke” Need to install Cucumber Gem –Just for test and development environment, not for production environment When Cucumber installed, it creates commonly used step definitions Need a test database to run app Then edit.features file to add features 36

Fake User to Try Scenarios? Need tool that pretends to be the user to follow scenarios of user stories Capybara simulates browser –Can interact with app to receive pages –Parse the HTML –Submit forms as a user would 37

38 END

Steps use Given for current state, When for actions, and Then for consequences of actions Cucumber matches step definitions to scenario steps using regexes, and Capybara pretends to be a user that interacts with the SaaS app accordingly Step definitions are in Ruby, and are similar to method calls, while steps are in English and are similar to method definitions 39 Which is FALSE about Cucumber and Capybara?

40 END

Running Cucumber and Capybara (Engineering Software as a Service §7.7) 41 © 2013 Armando Fox & David Patterson, all rights reserved

Red-Yellow-Green Analysis 42

Demo Add feature to cover existing functionality –Note: This example is doing it in wrong order – should write tests first –Just done for pedagogic reasons (Or can look at screencast: 43

44 END

Enhancing Rotten Potatoes Again (Engineering Software as a Service §7.8) 45 © 2013 Armando Fox & David Patterson, all rights reserved

Add a Real New Feature? What if we add something harder? –e.g., includes form to fill in –e.g., needs a User Interface –e.g., needs to add route to connect view to controller –e.g., includes both a happy path and a sad path 46

Integrated with The Movie Database (TMDb) New Feature: Populate from TMDb, versus enter information by hand Need to add ability to search TMDb from Rotten Potatoes home page Need LoFi UI and Storyboard 47

Storyboard TMDb Figure 7.6 of Engineering Software as a Service 48

Search TMDb User Story (Fig. 7.7 ESAAS) Feature: User can add movie by searching in The Movie Database (TMDb) As a movie fan So that I can add new movies without manual tedium I want to add movies by looking up their details in TMDb Scenario: Try to add nonexistent movie (sad path) Given I am on the RottenPotatoes home page Then I should see "Search TMDb for a movie" When I fill in "Search Terms" with "Movie That Does Not Exist" And I press "Search TMDb" Then I should be on the RottenPotatoes home page And I should see "'Movie That Does Not Exist' was not found in TMDb." 49

Haml for Search TMDb Page (Fig. 7.8 ESAAS) -# add to end of app/views/movies/index.html.haml: %h1 Search TMDb for a movie = form_tag :action => 'search_tmdb' do %label{:for => 'search_terms'} Search Terms = text_field_tag 'search_terms' = submit_tag 'Search TMDb' 50

Haml Expansion Last Two Lines This Haml: = text_field_tag 'search_terms' = submit_tag 'Search TMDb’ Turns into this HTML: Search Terms for attribute of label tag matches id attribute of input tag, from text_field_tag helper (above) 51

Try Cucumber? If try Cucumber, it fails Missing the route Also MoviesController#search_tmdb is controller action that should receive form, yet not in movies_controller.rb Should use Test Driven Development (future lecture) to implement method search_tmdb Instead, to finish sad path, add fake controller method that always fails 52

Trigger Fake Controller When Form is POSTed (Fig. 7.9) # add to routes.rb, just before or just after 'resources :movies' : # Route that posts 'Search TMDb' form post '/movies/search_tmdb' 53

Fake Controller Method: Will Fail Finding Movie (Fig. 7.9) # add to movies_controller.rb, anywhere inside # 'class MoviesController < ApplicationController': def search_tmdb # hardwired to simulate failure flash[:warning] = "'#{params[:search_terms]}' was not found in TMDb." redirect_to movies_path end 54

55 END

56 Which statement is TRUE? A sad path can pass without having code written needed to make a happy path pass None of the above is true Usually you complete the Behavior Driven Design phase with Cucumber before starting the Test Driven Development phase with RSpec

57 END

Running Rotten Potatoes Again (Engineering Software as a Service §7.8) 58 © 2013 Armando Fox & David Patterson, all rights reserved

Demo Add feature to search for movie in TMDb –Note: This will be a sad path, in that won’t find it –Will use fake method (until future when implement it using TDD) (Or can look at screencast: 59

Happy Path of TMDb Find an existing movie, should return to Rotten Potatoes home page But some steps same on sad path and happy path How to make it DRY? Background means steps performed before each scenario 60

TMDb w/2 Scenarios: Background (Fig. 7.10) Feature: User can add movie by searching for it in The Movie Database (TMDb) As a movie fan So that I can add new movies without manual tedium I want to add movies by looking up their details in TMDb Background: Start from the Search form on the home page Given I am on the RottenPotatoes home page Then I should see "Search TMDb for a movie” Scenario: Try to add nonexistent movie (sad path) When I fill in "Search Terms" with "Movie That Does Not Exist" And I press "Search TMDb" Then I should be on the RottenPotatoes home page And I should see "'Movie That Does Not Exist' was not found in TMDb.” Scenario: Try to add existing movie (happy path) When I fill in "Search Terms" with "Inception" And I press "Search TMDb" Then I should be on the RottenPotatoes home page And I should see "Inception” 61

Cucumber Summary New feature => UI for feature, write new step definitions, even write new methods before Cucumber can color steps green Usually do happy paths first Background lets us DRY out scenarios of same feature BDD/Cucumber test behavior; TDD/RSpec in folllowing chapter is how write methods to make all scenarios pass 62

63 END

And in Conclusion Cucumber – “magically” maps 3x5 card user stories onto acceptance tests and integration tests for the application 64