Skip to content

Instantly share code, notes, and snippets.

@allenan
Last active August 29, 2015 14:14
Show Gist options
  • Save allenan/75be302f0c2c310085f3 to your computer and use it in GitHub Desktop.
Save allenan/75be302f0c2c310085f3 to your computer and use it in GitHub Desktop.
# 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