eWorm with jQuery and JSON
I have added to my eWorm Rails Web App in a few feature branch. This addition focuses on using jQuery scripts and a JSON API backend to load and update data on the server without needing to refresh the page.
I created a JSON backend, which is very easy with rails! My books serializer looks like this:
And then I just had to create simple serializers for the other models like so:
After this, is it was just a matter of updating a few controller actions to allow access via JSON like so:
I added an additional feature, a genre filter on the books index page. It’s a nice fast, and clean way to filter the books by genre without refreshing the page! Check out this code snippet:
To help keep my code neat, I wrote my functions inside app/assets/javascripts/books.js and just called the necessary functions on the appropriate views:
I on the books index page, I also implemented a form that could be toggled viewable, and when submitted would auto-magically add the new book to the page! This was fun and I could see this as a practical feature for some websites as it could reduce the time it takes to add content.
This was challenging because Javascript and jQuery have their quirks that I find quite annoying coming from months of ruby. I found it difficult to store the results of AJAX requests in a variable, so I just had to avoid it. Overall this project was a unique challenge and a great learning experience as I move forward in my Learn Verified adventure!