Remember the OMDB Lab? In the third week of class we used AJAX to call OMDB's API for movie information. We're going to do a similar lab, but this time we're going to incorporate our newfound server-side knowledge.
- Do all of the OMDB API interaction from within Rails. Any AJAX requests you make should only be from the browser to your server.
- Use the Typheous library to call the OMDB API from Rails.
- Create a search page that allows you to search for movies.
- As an improvement, use AJAX to call your server, which will call OMDB, and send JSON back to the browser than can be used to render the search results.
Building a Rails app and using Typhoeus make OMDB API requests.
- Create a new Rails app.
- Add the
typhoeus
gem to yourGemfile
. - Create a
movies
controller. - In your
movies/index.html.erb
template create a search form. Read the best practices for creating search forms in Section 1.1 - In your
movies_controller.rb
have an action handle the form submission and grab the search term the user entered. - Search OMDB for movies with that title using Typheous.
- The results will be returned as a JSON string. Convert it into a Ruby object.
- Pass the search results to a template you create and render the list of results in the template.
You should have a (minimally) functional site after Part 1. However, we did not use any AJAX nor did we have our Rails app act as a JSON API. Let's do both of those things now.
Instead of having your search form cause a page refresh, use JavaScript to make an AJAX request to your server (which will then call OMDB). Then, instead of your server rendering the results in a template, it should return them as JSON to the browser. Use JavaScript to render the results (either with jQuery, or using Handlebars, if you want to be fancy).
- Rename
movies.js.coffee
tomovies.js
and, - DELETE THE COMMENTS. Rails generates CoffeeScript files by default,
but we don't want to use CoffeeScript.
#
is not the comment syntax in JavaScript, so we need to delete those too. - Rails provides
jQuery
for us (see for yourself inapplication.js
) so you can just start using it. - Use
jQuery
to listen for thesubmit
event on the form. Prevent the page from taking it's default action. - Use one of
$.ajax()
,.get()
, or$.getJSON()
to call your server with the search term the user typed in. - In your controller, search OMDB just as you did in Part 1, but this
time use
respond_to
to return JSON to the browser. - Use
jQuery
to render the list of movies client-side.
Use Handlebars
to render the list of movies after your AJAX
request. Check out
handlebars_assets if
you're interested in integrating Handlebars templates with your Rails
application.
Implement the movies#show
page that will give more information on a
specific movie.