Routing, Controllers, Ruby

In Review

  • Static pages and HTTP verbs introduced

Today

  • What goes on in a web request
  • More about routing and controllers

Which of these would probably use a GET request?

  1. Submitting a form to register on Facebook
  2. Hitting the "Done" button when updating your Facebook profile
  3. Clicking the Facebook logo to go back to the Newsfeed
  4. Clicking "Unfriend" on your backstabbing friend's profile.

How It All Works

  • Let's talk about routing

Live Coding (Routes)

http://bit.ly/lec2-1

Visiting which URL in the browser will cause an error?

  1. localhost:3000/
  2. localhost:3000/hello
  3. localhost:3000/foobar
  4. localhost:3000/bye

Controllers

  • routes.rb sends the job over to a controller
  • Controller sets variables for use in view

 

  • params variable automatically set from URL

Live Coding (Controller, View)

http://bit.ly/lec2-2

Ruby

  • Everything is an object
    • "hello".length()
    • 2.weeks
    • 3.days.from_now
  • Like Python, but nicer to write
    • Use Google + Stack Overflow as you get the hang of things

    • Eg. "How to iterate over an array"

  • Poetry mode means parens + brackets sometimes optional, return optional for last line

Ruby Classes

  • OOP in Ruby works much like in Python
  • Use @foo instead of self.foo
  • All variables private by default
class Sam
  def initialize(coolness)
    @cool = coolness * coolness
    foobar = coolness * 2 # disappears!
  end

  def cool
    @cool
  end

  def self.awesome?
    true
  end
end
me = Sam.new 10
me.foobar # error
me.cool # 10
me.awesome? # error
Sam.awesome? # true

The Problem

  • Making requests is great and all, but requests are stateless!
  • How can we start incorporating state into our app?
  • Stay tuned for next time when we introduce models!

Let's Get to the Code!