Databases & Rails: Week 5 Controllers
13 Jul 2012We’ve covered models, views, routing, and briefly introduced controllers. This week we’re taking the deep dive into controllers, talking about what they can do, and how we want to use them. Last week we talked about wiring things together with routing. You can watch the Recap then do the Quiz and once you’re done check out the Quiz Solution.
Before we get started talking about controllers, first we’ll introduce Ruby Variable Scope. We’ve used local variables and constants in our code, and now it’s time to introduce instance variables and discuss their differences, and why you might want to use one over another.
Now that you’ve seen how we can use instance variables, we will use one in our controller to move some SQL logic from the view to the controller. This is important for keeping our views clean and focused on presentation logic. It will also help us later when we look into the other things a controller can do.
Now that we’re pulling data from our database in our controller we can render, redirect Filter & Format from our controllers. Check out this video to find out what these are and how we can use them to help build our app.
This last video on Rails URL Helpers covers using $ rake routes
to get info on routes and how we can use different url helpers in our app.
In the exercise we’ll be nesting a few if
, def
and class
definitions in our code. It can be easy to lose your place and difficult to remember what end
corresponds to which line. To help out you can watch a quick video on my End of the line, programming trick with the sublime text editor.
Exercise
In this exercise we’re fleshing out the app we worked with later to add full CRUD and move logic from the view to the controller. Just fork and clone the repo and go through the readme!
Thanks for following along, for up to date ruby info you can catch me @schneems, and can subscribe to my youtube channel