All Work and No Play Makes AJAX a Dull Boy

Posted by warrend on June 26, 2017

I felt a bit like Jack in The Shining during this project, feeling like I was trapped in this big house in the mountains with forces I don’t understand. However, I finished my JQuery Rails project and learned a ton and made it out of the hedge maze alive.

I wired up AJAX requests for my user show pages, which also included a list of their loglines (a movie or TV show description.) For this, I created a JS user object and used Handlebars template to display the user show pages and all of their loglines. Within each logline, the user has the option to rate a logline 1-100, but you can’t rate your own loglines and you can only rate a logline once.

I had to use some logic in my JS file to loop through all of the logline object’s ratings and detect if any of the rating’s user_ids matched the current user id. This was much easier to do in Rails than JavaScript. Because I didn’t have access to certain data inside my JS file, I had to come up with ways of passing the JSON data and data attributes into a Handlebars helper. I quickly learned that Handlebars templates aren’t great at logic, in fact, I think they’re supposed to be pretty logic-less for the most part. The process became a bit tricky, but, to reference Mitch McConnell, I persisted and finished.

I learned a lot doing this project, but I’m also left with several questions that I’m sure I’ll figure out down the road. One issue I noticed was when I manually typed in a URL for a user, ‘/users/23’ for example, (23 being the number for the greatest player of all times, LeBron James, of course) the JSON viewer would immediately pop up. I’m guessing this is an issue with not having the show page setup. Also, when I used Ajax from an index page, I didn’t have access to the @user instance variable, I only had access to the index, which forced me to rely only on JSON. I’m not sure if there’s a way around this issue.

This project got me thinking a lot about design as well. Knowing how to use AJAX properly, I would have certainly designed my Rails project differently. Learning to use APIs and JSON feels a bit like graduating college — I feel like an adult coder now, but I also know there’s still so much to learn.