$(function () {
  $('#search_movie').submit(function(){
    var $form     = $(this);
    var movieName = $form.children('#movie_name_input').val();
    var movieYear = $form.children('#movie_year_input').val();

    var apiUrl    = $form.attr('action') + '/' + movieYear + '/' + movieName;

    $.ajax({
      url: apiUrl,
      beforeSend: function() {
        $('#loading').show();
      },
      success: function(movie){
        console.log(movie);
        var movie_html = '';

        movie_html += '<h3>' + movie.name + ' (' + movie.info.year + ')</h3>';
        movie_html += '<img src="' + movie.info.poster + '">';
        movie_html += '<h4>Combined Rating: ' + movie.combined_rating + '</h4>';

        var movie_ratings = '<h4>Ratings: </h4>';

        $.each(movie.ratings, function(rating_site, rating) {
          if (rating.error) {
            movie_ratings += '<h4>' + rating_site + '</h4>';
            movie_ratings += rating.error;
          } else {
            movie_ratings += '<h4><a href="' + rating.url + '">';
            movie_ratings += rating_site + '</a>:</h4> ' + rating.score;
          }
        });

        movie_html += movie_ratings;

        $('#loading').hide();
        $('#result').html(movie_html);
      }
    });
    return false;
  });

  $('#movie_name_input').autocomplete({
    source: function(request, response) {
      $.ajax({
        url: '/api/search',
        data: {
          term: request.term
        },
        success: function(data) {
          response($.map(data.movies, function(movie) {
            return {
              label: movie.name + ' (' + movie.year + ')',
              value: movie.name,
              year: movie.year
            }
          }));
        },
      });
    },
    select: function(event,ui) {
      $('#movie_year_input').val(ui.item.year);
    },
    minLength: 2
  });
});

@@ -49,16 +49,769 @@
 tion()%7B%0A
+    function generateHtmlFromMovie(movie) %7B%0A      var movieHtml = '';%0A%0A      movieHtml += '%3Ch3%3E' + movie.name + ' (' + movie.info.year + ')%3C/h3%3E';%0A      movieHtml += '%3Cimg src=%22' + movie.info.poster + '%22%3E';%0A      movieHtml += '%3Ch4%3ECombined Rating: ' + movie.combined_rating + '%3C/h4%3E';%0A%0A      var movieRatings = '%3Ch4%3ERatings: %3C/h4%3E';%0A      $.each(movie.ratings, function(rating_site, rating) %7B%0A        if (rating.error) %7B%0A          movieRatings += '%3Ch4%3E' + rating_site + '%3C/h4%3E';%0A          movieRatings += rating.error;%0A        %7D else %7B%0A          movieRatings += '%3Ch4%3E%3Ca href=%22' + rating.url + '%22%3E';%0A          movieRatings += rating_site + '%3C/a%3E:%3C/h4%3E ' + rating.score;%0A        %7D%0A      %7D);%0A      movieHtml += movieRatings;%0A      return movieHtml;%0A    %7D%0A%0A
     var 
@@ -831,16 +831,16 @@
 (this);%0A
-
     var 
@@ -957,17 +957,16 @@
 .val();%0A
-%0A
     var 
@@ -1182,816 +1182,115 @@
     
-console.log(movie);%0A        var movie_html = '';%0A%0A        movie_h
+movieH
 tml 
-+
 = 
-'%3Ch3%3E' + movie.name + ' (' + movie.info.year + ')%3C/h3%3E';%0A        movie_html += '%3Cimg src=%22' + movie.info.poster + '%22%3E';%0A        movie_html += '%3Ch4%3ECombined Rating: ' + movie.combined_rating + '%3C/h4%3E';%0A%0A        var movie_ratings = '%3Ch4%3ERatings: %3C/h4%3E';%0A%0A        $.each(movie.ratings, function(rating_site, rating) %7B%0A          if (rating.error) %7B%0A            movie_ratings += '%3Ch4%3E' + rating_site + '%3C/h4%3E';%0A            movie_ratings += rating.error;%0A          %7D else %7B%0A            movie_ratings += '%3Ch4%3E%3Ca href=%22' + rating.url + '%22%3E';%0A            movie_ratings += rating_site + '%3C/a%3E:%3C/h4%3E ' + rating.score;%0A          %7D%0A        %7D);%0A%0A        movie_html += movie_ratings;%0A%0A        $('#loading').hide();%0A        $('#result').html(movie_html
+generateHtmlFromMovie(movie);%0A        $('#result').html(movieHtml);%0A        $('#loading').hide(
 );%0A 

Refactor script.js