w4d5 - Reddit? I barely even know it!
Week 4 done! We’ve officially been coding at App Academy for a month, and it’s amazing to see how much we’ve learned since then. Today’s project was to build a version of Reddit from the group up, and though it was surely a challenge, it’s something that we were able to manage, which we could not have done even a few days earlier in the week. The rate at which we are learning somehow seems to be speeding up. I would not have believed you if you’d told me that at the end of Week 1. But here we are, 1/3 of the way into the program and running at full speed. We are at a point where we’re starting to see how everything in Rails fits together nicely under the “MVC” architecture, and the past week was pretty much dedicated to getting familiar with the different pieces, as well as seeing how they all interact.
The models represent the “M" in the holy trinity of the "MVC" application architecture, and they are probably the most straightforward of the three. They enable you to interact with the database on the backend of your application via an Object Oriented means. This abstracts much of the SQL querying that goes on beneath the surface. Working on the ActiveRecord Lite project last week gave us exposure to the way that this is actually done, and gave me a huge appreciation for the existence of ActiveRecord! It’s easy to see how complex things could get if you had to interact with the database directly every time you needed access to the information.
The other pieces of “MVC” are the Views and Controllers, which each come with their fair share of complexity. The Views are basically the web pages that your application uses, and from the client side are made up of HTML. In reality, producing these views requires some finesse, using Embedded Ruby (ERb), to dynamically generate the HTML before it gets sent to the server (and the client’s browser). This has probably been my least favorite part of dealing with Rails so far. HTML just seems so clunky and we are tasked with shoehorning Ruby into it, in order to get results. I’m left wondering if things are going to get any cleaner when we start to incorporate Javascript, but I doubt it!
Controllers, on the other hand have been fun to work with. They are one of the most “magical” parts of Rails, and they do a LOT behind the scenes with very minimal setup. But once you start understanding them, you start to realize just how powerful they can be. Controllers sit in the middle of every request to your application, and their job is to control things (big surprise). They give the Views information about the Model that helps generate the HTML. The take information passed from the View and give it to the Model to make changes in the database. They also control the level of access that clients have to any part of your application. In a nutshell, they receive a request from a clients browser (via the router built specifically for handing things off to the Controller), they decide how to handle the request, and when they’re done, they tell the client’s browser what to do next.
Having knowledge of all of these pieces was a huge help for constructing a stripped down version of Reddit, which was our task on Friday (especially since neither myself nor my partner were Reddit pros). Once again, we had to build a user authentication system first, which wasn’t too bad. We implemented the User model with the necessary fields for authentication, and it took us way less time than it did for either of the previous 2 projects. During User authentication, we also started tying together the Users Controller and the Views for the User pages. We worked in this sort of circular pattern for most of the day, creating a model, building it’s associations to other models, building it’s controller with the necessary actions, and then fleshing out the views which allowed us to see our hard work come to fruition. Doing things in this way, just seemed like a natural way to flow through the application, and it seemed like doing one thing just sort of led to the next, always giving us something to work towards. By the end of class, we had a working application that included Users, Subs (subreddits), Posts, and Comments. Later on, I went back and did some refactoring to optimize the rendering of nested comments, and also introduced a basic voting system where users could upvote or downvote individual posts or comments. To do this, I had to implement a Votes resource, using polymorphic associations so that both posts and comments were “votable”.
As we move into Week 5, I feel like we’re capable of putting together pretty complete web apps, at least basic ones. Of course, we have not delved into CSS yet, which will enable us to add some style, and Javascript which will allow us to script some advanced functionality… but the basic knowledge is coming together, and the first half of this week will focus on solidifying that knowledge as we finish up Rails and move into the part of the curriculum focused on Javascript.