Created
May 20, 2014 20:20
-
-
Save catm705/2778e238e9536a5afc21 to your computer and use it in GitHub Desktop.
AJAX API calls in RAILS
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
--------------------------------- | |
---- searches_controller.rb ----- | |
--------------------------------- | |
def create | |
# 1.) js file | |
# 2.) click event to prevent default route to the controller from js with the url. | |
# get value from form | |
# ajax request to send form data | |
# /searches - 'create method' POST | |
# which comes HERE | |
# Then render the JSON | |
# Then when 'done' back in js | |
# Take response render in html. | |
@user = User.find(current_user.id) | |
date_query = URI.escape(params[:date_input]) | |
@results = User.search_events(date_query) | |
# respond_to do |format| | |
# format.html { redirect_to searches_path } | |
# format.json { render json: @results } | |
# end | |
render :index | |
end | |
--------------------------------- | |
---------- user.rb -------------- | |
--------------------------------- | |
def self.search_events(date_input) | |
data = HTTParty.get("http://api.eventful.com/json/events/search?app_key=#{ENV['APP_KEY']}&date=#{date_input}&location=New+York&within=15&page_size=20&include=price") | |
json_data = JSON.parse(data) | |
results = [] | |
if json_data.nil? || json_data.empty? | |
flash[:error] = "Data not found" | |
return false | |
else | |
json_data["events"]["event"].each do |event| | |
single_event = {} | |
single_event[:image_url] = event["image"] ? event["image"]["medium"]["url"] : "" | |
single_event[:title] = event["title"] | |
single_event[:url] = event["url"] | |
single_event[:price] = event["price"] | |
single_event[:address] = event["venue_address"] | |
single_event[:city_name] = event["city_name"] | |
single_event[:region_name] = event["region_name"] | |
single_event[:start_time] = event["start_time"] | |
single_event[:description] = event["description"] | |
results << single_event | |
end | |
end | |
results | |
end | |
--------------------------------- | |
---------- events.js ------------ | |
--------------------------------- | |
Change to AJAX request later?.... | |
$(document).ready(function(){ | |
$('#general_event_search form').on('submit', function(event){ | |
// alert("Did this work?"); | |
event.preventDefault(); | |
var event_search_date = $('option').val(); | |
$.ajax({ | |
url: "/searches", //searches controller - not about page! | |
type: 'POST', | |
dataType: 'json', | |
data: {date_input: event_search_date} | |
}).done(function(data){ | |
$.each(data, function(index, value){ | |
var event_div = $('<div>').text(value); | |
$('#general_event_results').append(event_div); | |
}); | |
}); | |
}); | |
}); | |
--------------------------------- | |
---------- Search Form ---------- | |
--------------------------------- | |
<div class="search" id="general_event_search"> | |
<%= form_tag("/searches", method: "POST") do %> | |
<%= label_tag(:date, "Date") %> | |
<%= select_tag(:date_input, options_for_select([['Future', 'Future'], ['Today','Today'],['This Week', 'This Week'], ['Next Week', 'Next Week']])) %> | |
<br> | |
<%= submit_tag("Search") %> | |
<% end %> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment