I am new to ruby on rails. On our site user gets to search and then get a list of results.
Now I want to add the ability to view results sorted (by price, rating, etc). The API does the sorting so all I have to do is sent a new post request to API with the new option (i.e. &sort=PRICE ) then it will return results in order.
My issue is I don't know how to do this, I've tried button_tag
and button_to
, with button_to
if clicked and then manually refreshed it works, but if I add multiple button_to
tags it won't.
I have made $sort
as global variable (with default sort value in API docs i.e. no_sort) so when a button is pressed I try to change that variable to desired name then post, this is what I have in view:
<%= button_to 'price', :controller => 'database', :action => 'getList', :method => 'post' do %>
<% $sort="PRICE" %>
<% end %>
<%= button_to 'rating', :controller => 'database', :action => 'getList', :method => 'post' do %>
<% $sort="QUALITY" %>
<% end %>
<%= button_to 'distance', :controller => 'database', :action => 'getList', :method => 'post' do %>
<% $sort="PROXIMITY" %>
<% end %>
my controller :
def getList()
#search parameters
destinationString = params[:poi].gsub(' ', '+')
propertyName = params[:name].gsub(' ', '+')
stateProvinceCode = params[:state].gsub(' ', '+')
city = params[:city].gsub(' ', '+')
arrival = DateFormat(params[:start_date])
departure = DateFormat(params[:departure])
@arrivalDate = params[:start_date]
@departureDate = params[:departure]
#construct http request
request = $gAPI_url + "/list?" \
+ "cid=" + $gCid \
+ "&apiKey=" + $gApiKey \
+ "&sort=" + $sort \
+ "&numberOfResults=" + $gNumberOfResults \
+ "&destinationString=" + destinationString \
+ "&propertyName=" + propertyName \
+ "&stateProvinceCode=" + stateProvinceCode \
+ "&city=" + city \
+ "&arrivalDate=" + arrival \
+ "&departureDate=" + departure
response = JSON.parse(HTTParty.get(request).body)
puts request
# Check for EanWsError
if response["HotelListResponse"]["EanWsError"] then
hotelError = response["HotelListResponse"]["EanWsError"]
#Multiple possible destination error.
if hotelError["category"] == $gERROR_CATEGORY_DATA_VALIDATION then
#create list of suggested destinations
#We are not yet implementing the suggestions list functionality.
#@destinationList = []
#@destinationListSize = Integer(response["HotelListResponse"]["LocationInfos"]["@size"]) -1
#(0..(@destinationListSize)).each do |i|
#destinationInfo = response["HotelListResponse"]["LocationInfos"]["LocationInfo"][i]
#@destinationList << Destination.new(destinationInfo)
#end
redirect_to '/database/errorPage'
#No results were returned
elsif hotelError["category"] == $gERROR_CATEGORY_RESULT_NULL then
#TODO: Figure out what to do if no results were found.
# Currently sending them back to the homepage
redirect_to '/database/errorPage'
end
#We got a valid response. Parse the response and create a list of hotel objects
else
#Our hotelListSize is subtracted by 1 because we only want up to last index
@hotelList = []
@hotelListSize = Integer(response["HotelListResponse"]["HotelList"]["@size"]) -1
(0..(@hotelListSize)).each do |i|
hotelSummary = response["HotelListResponse"]["HotelList"]["HotelSummary"][i]
@hotelList << Hotel.new(hotelSummary)
@hotelList[i].thumbNailUrl = "http://images.travelnow.com" + response["HotelListResponse"]["HotelList"]["HotelSummary"][i]["thumbNailUrl"]
end
end
end
or could something simpler work?, like:
<input type="button" value="Reload Page" onClick="document.location.reload(true)";"$sort=PRICE"> >
Short part
You have several problems, but to solve your main problem, try:
In your controller:
Long part
After short part works.