$(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
  });
});

$(function () {
  $('#search_movie').submit(function(){
    function generateHtmlFromMovie(movie) {
      var movieHtml = '';

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

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

    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){
        movieHtml = generateHtmlFromMovie(movie);
        $('#result').html(movieHtml);
        $('#loading').hide();
      }
    });
    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
  });
});

Refactor script.js