{"id":1174,"date":"2013-10-07T22:11:44","date_gmt":"2013-10-08T05:11:44","guid":{"rendered":"http:\/\/zackmdavis.net\/blog\/?p=1174"},"modified":"2013-10-14T23:45:56","modified_gmt":"2013-10-15T06:45:56","slug":"app-academy-diary-week-four","status":"publish","type":"post","link":"http:\/\/zackmdavis.net\/blog\/2013\/10\/app-academy-diary-week-four\/","title":{"rendered":"App Academy Diary, Week Four"},"content":{"rendered":"<p><span id=\"monday\"><em>Monday 7 October 2013&mdash;<\/em><\/span> The main part of SedentaryRecord went pretty smoothly. I like my one-liner implementation of the <code>has_many_through<\/code> association better than the instructions' suggestion of writing a whole new query template; the TA Patrick pointed out that my version is inefficient (firing off two queries rather than one), but instead of writing the query-saving long version right away, I decided to try implementing validations first (one of the suggested extension ideas). That didn't go well at <em>all<\/em>; I spent a lot of time ineffectually hacking away at the problem but didn't even come up with anything worth committing!<\/p>\n<p><span id=\"wednesday\"><em>Wednesday 9 October 2013&mdash;<\/em><\/span> I did poorly on the assessment yesterday. There were eight SQL queries to write; the first five were trivial, but I bombed the last three because <a href=\"http:\/\/zackmdavis.net\/blog\/2013\/08\/im-a-moron\/\">I'm a moron<\/a> and didn't remember that the keyword for filtering aggregations was HAVING. I worked with <em>William Ott<\/em> on an ice-cream finder (which uses Google Maps APIs to print out directions to nearby ice-cream) and a Twitter client. Of course, it simply wouldn't do to write an ice-cream finder without using it to find ice-cream, so after class we took one of our program's suggestions and bought ice-cream at the H\u00e4agen-Dazs in the mall on Market and Fifth. Today I worked with <em>David A.<\/em>; we learned about routers and controllers.<\/p>\n<p><!--more--><a href=\"http:\/\/zackmdavis.net\/blog\/wp-content\/uploads\/2013\/10\/our_cat_for_rent.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/zackmdavis.net\/blog\/wp-content\/uploads\/2013\/10\/our_cat_for_rent-129x300.png\" alt=\"Our Cat for Rent\" width=\"129\" height=\"300\" class=\"alignleft size-medium wp-image-1182\" srcset=\"http:\/\/zackmdavis.net\/blog\/wp-content\/uploads\/2013\/10\/our_cat_for_rent-129x300.png 129w, http:\/\/zackmdavis.net\/blog\/wp-content\/uploads\/2013\/10\/our_cat_for_rent.png 172w\" sizes=\"(max-width: 129px) 100vw, 129px\" \/><\/a><\/p>\n<p><span id=\"thursday\"><em>Thursday 10 October 2013&mdash;<\/em><\/span> Dear reader, suppose <a href=\"http:\/\/zackmdavis.net\/blog\/2013\/09\/app-academy-diary-week-two\/#sunday\">yet<\/a> <a href=\"http:\/\/zackmdavis.net\/blog\/2013\/09\/app-academy-diary-week-three\/#saturday\">again<\/a> that you're working on the great American dog-sharing site\u2014<em>loser!<\/em> Sharing dogs is <em>so<\/em> last week; you should have predicted that by now, everyone who's anyone would be renting cats instead. That's why today <em>Dean Yang<\/em> and I used our prodigious knowledge of not just models, but also both views <em>and<\/em> controllers, to make a mock cat-rental site! It's like this: say you visit the URL for the page that displays all the cats. Your browser issues an HTTP GET request, which is handled by the Rails router: an instance of the <code>CatsController<\/code> is spawned, and its <code>index<\/code> method is called, which puts an array of <code>Cat<\/code> objects (instantiated by the ActiveRecord ORM from their representations in the database) in an instance variable, which is then made available to a process that uses a template to decide how to express the information about the cats in HTML, which is then sent back to your browser. Similar stuff happens to let you make a new cat, or edit an existing cat.<\/p>\n<p><span id=\"sunday\"><em>Sunday 13 October 2013&mdash;<\/em><\/span> On Friday I worked with <em>Nathan Holland<\/em> <a href=\"http:\/\/zackmdavis.net\/blog\/2013\/09\/app-academy-diary-week-two\/#wednesday\">again<\/a>; we added users and authentication to the cat-rental site! It's like this: in the app\/config directory of the source repository there lives a file named <em>routes.rb<\/em>, which which tells Rails what to do with requests! Like, <em>routes.rb<\/em> contains a line that says &quot;<code>get 'login', :to =&gt; 'sessions#new'<\/code>&quot;, which means that if someone issues an HTTP GET request to \/login at our application's domain, that what happens next is determined by the <code>new<\/code> method in the <code>SessionsController<\/code>. As it happens, all that does is send back the log-in form generated from the template at app\/views\/sessions\/new.html.erb. When the user submits the form, a POST request is issued to \/login, which gets routed to the <code>create<\/code> method in the <code>SessionsController<\/code>! That does a few things. First, it tries to find the user in the database using the supplied credentials (username and hash-of-password). If that doesn't work, it shovels a friendly incorrect-username-or-password message onto <code>flash[:errors]<\/code> and renders the log-in page again (including the message from the <code>flash<\/code> hash). But if the user <em>was<\/em> found, then it logs them in by setting their session token, storing their current-user status in an instance variable, shoveling a friendly &quot;Logged in from #{location}&quot; message onto <code>flash[:messages]<\/code> (the user's location can be determined using their IP and the <a href=\"http:\/\/www.rubygeocoder.com\/\">Geocoder gem<\/a>), and redirecting to the page with all the cats!<\/p>\n<p>Having accomplished this feat, Nathan and I spent a lot of time trying to push the application to <a href=\"https:\/\/www.heroku.com\/\">Heroku<\/a> so that we could test things like how to handle a user being logged in on several devices at once, but Heroku dislikes SQLite, and various attempts at troubleshooting the issue all failed horribly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Monday 7 October 2013&mdash; The main part of SedentaryRecord went pretty smoothly. I like my one-liner implementation of the has_many_through association better than the instructions' suggestion of writing a whole new query template; the TA Patrick pointed out that my &hellip; <a href=\"http:\/\/zackmdavis.net\/blog\/2013\/10\/app-academy-diary-week-four\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[64],"_links":{"self":[{"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/posts\/1174"}],"collection":[{"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/comments?post=1174"}],"version-history":[{"count":11,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/posts\/1174\/revisions"}],"predecessor-version":[{"id":1195,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/posts\/1174\/revisions\/1195"}],"wp:attachment":[{"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/media?parent=1174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/categories?post=1174"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zackmdavis.net\/blog\/wp-json\/wp\/v2\/tags?post=1174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}