Intro to Rails (Fri Sep 15, lecture 7)

Homework due for today

Legend: : Participation | : Early | : PDF | : Portfolio | : Zipped

  1. RORT: Read Ruby on Rails Tutorial ([RORT]) Chapter 2. Look at Rails 5.1 Notes and Caveats for late breaking addenta and errata. This is going to be another exciting chapter. Don’t worry if a lot of things seem “magic”, as the classes continue it will all become crystal clear to you! For this chapter you are going to create a toy application. Make sure you understand what the app is trying to do and actually follow along. I strongly recommend that you type in the code just like what he says in the book because from my experience youy will learn a lot more that way. You will use the rails command and the rake command as part of your development process. You will for the first time start the process of understanding really how a web server actually works! Take your time and see if you can make it all the way to the end and get it all to work. You will hit snags. Use your resources: the TAs, the class mailing list, stack overflow, google and push through to success! When you are done, go to Latte and answer these warmup questions:

    • What is a url and what role does it play in the MVC architecture and the basic operation of a web server?
    • What job does a view and a model do in the MVC architecture?
    • Complete this sentence: “Rails is …”
    • The chapter talks about “deploying” the rails application. What do you think that means?
    • Are there one or two things you are still confused by? If not, what one or two things were the most memorable about your reading? Deliverable: Do the warmup in Latte and submit by 10am on the day of class.
  2. PA Movies 2: Continue working on (PA) Movies Part 2. It’s not super easy so don’t wait till the last minute!

Homework Discussion

  • How did it go with Chapter 2?
  • What is an ORM?
  • What is MVC?

How a web server works

  • Review of some basic concepts
    • Client and Server
    • Client asks for a page; server sends a page
    • Web server receives the request and creates the page and sends it back
  • A level deeper:
    • Server (usually) looks inside a database for information
    • And runs an algorithm on that to create the output
    • And that’s what gets sent back.

HTTP is a network protocol

  • Mental model: calling a function across a network
    • Client is the caller
    • Server is where the function exists
    • Client receives the result
Let’s use telnet to make such a ‘call’
$ telnet 80
$ GET / HTTP/1.1
$ Host:
Deconstructing the call
  • “Call” includes:
    • target host (
    • target port on the host (80)
    • HTTP verb: “GET” (a.k.a. method) (ref: HTTP Method Definitions)
    • address: (the path or “/”)
    • Version of HTTP protocol (1.1)
  • There are lots of other “parameters” that are allowed in this call
Deconstructing the returned data from that call
  • (Reference: HTTP Response Fields)
  • “Response” includes:
    • Status code: 200 (see [HTTP Status Codes(])
    • Accept-Ranges: bytes (lets the server tell the caller that it has that capability)
    • Cache-Control: max-age=604800, how long this response may be cached)
    • Content-Type: text/html Tells recipient how to parse the result)
    • Date: Thu, 27 Feb 2014 00:33:26 GMT The current date time on the server)
    • Etag: “359670651” kind of a unique-id for caching purposes)
    • Expires: Thu, 06 Mar 2014 00:33:26 GMT (says when the page is required to expire)
    • Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT (When this page was edited)
    • Server: ECS (ewr/1584) (Software running the server)
    • X-Cache: HIT (Page came out of a cache)
    • x-ec-custom-error: 1 (There was no error in the cache)
    • Content-Length: 1270 (Content following this is 1270 bytes)
  • All this is followed by text that we know should be interpretted as text/html

What are the HTTP Methods

  • Think of them like a reguired first argument to the call
  • The interpretation of the

Simplified view of the world

  • Think of the relationship between the client (the user in a browser) and the server (the rails server) as a program calling a function!
    • Call: CallUsingHTTP(“GET”, “”, “/index.html”)
    • Returns: Status code, Content type, Content, and potentially more
  • Simply to accept HTTP requests
  • Decode them (“routing”)
  • Generate the necessary HTML (“views”)
  • Return the html as a response to the request (“Controller”)

Rails Points of interest

  • So what is Rails, really? It’s a library of Ruby code, that’s all
  • Why do they call Rails ‘opinionated’ software?
  • Go through the directory structure?
  • Key files and directories
    • Routes.rb
    • Models / Resources
    • Views
    • Controllers

Next Class