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?
-
Submitting a form to register on Facebook
-
Hitting the "Done" button when updating your Facebook profile
-
Clicking the Facebook logo to go back to the Newsfeed
-
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?
-
localhost:3000/
-
localhost:3000/hello
-
localhost:3000/foobar
-
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!
Spring 2015 - Week 3: Routing, Controllers, Ruby
By Rails Decal
Spring 2015 - Week 3: Routing, Controllers, Ruby
- 1,218