Last active
August 29, 2015 14:14
-
-
Save allenan/75be302f0c2c310085f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# routes.rb | |
Rails.application.routes.draw do | |
root 'trailers#index' | |
resources :trailers do | |
collection do | |
get 'query' | |
end | |
end | |
end | |
# app/controllers/trailers_controller.rb | |
class TrailersController < ApplicationController | |
def index | |
end | |
def query | |
response = HTTParty.get("http://api.traileraddict.com/?film=the-dark-knight&count=8") | |
trailer = response.parsed_response["trailers"]["trailer"][0]["embed"] | |
respond_to do |format| | |
format.json { render json: { trailer_html: trailer } } | |
end | |
end | |
end | |
# app/views/trailers/index.html.erb | |
<h1>Trailers</h1> | |
<%= form_tag query_trailers_path, method: 'get', remote: true, class: 'search-form' do |f| %> | |
<%= label_tag do %> | |
Search for trailers: <%= search_field_tag :query %> | |
<% end %> | |
<%= submit_tag :search %> | |
<% end %> | |
<div id="results"> </div> | |
<script> | |
$('.search-form') | |
.on('ajax:success', function(event, data) { | |
$('#results').html(data.trailer_html); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment