javascript - Ruby on rails, new POST to API and refresh page -
i new ruby on rails. on our site user gets search , list of results.
now want add ability view results sorted (by price, rating, etc). api sorting have sent new post request api new option (i.e. &sort=price ) return results in order.
my issue don't know how this, i've tried button_tag
, button_to
, button_to
if clicked , manually refreshed works, if add multiple button_to
tags won't.
i have made $sort
global variable (with default sort value in api docs i.e. no_sort) when button pressed try change variable desired name post, have in view:
<%= button_to 'price', :controller => 'database', :action => 'getlist', :method => 'post' %> <% $sort="price" %> <% end %> <%= button_to 'rating', :controller => 'database', :action => 'getlist', :method => 'post' %> <% $sort="quality" %> <% end %> <%= button_to 'distance', :controller => 'database', :action => 'getlist', :method => 'post' %> <% $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 eanwserror if response["hotellistresponse"]["eanwserror"] hotelerror = response["hotellistresponse"]["eanwserror"] #multiple possible destination error. if hotelerror["category"] == $gerror_category_data_validation #create list of suggested destinations #we not yet implementing suggestions list functionality. #@destinationlist = [] #@destinationlistsize = integer(response["hotellistresponse"]["locationinfos"]["@size"]) -1 #(0..(@destinationlistsize)).each |i| #destinationinfo = response["hotellistresponse"]["locationinfos"]["locationinfo"][i] #@destinationlist << destination.new(destinationinfo) #end redirect_to '/database/errorpage' #no results returned elsif hotelerror["category"] == $gerror_category_result_null #todo: figure out if no results found. # sending them homepage redirect_to '/database/errorpage' end #we got valid response. parse response , create list of hotel objects else #our hotellistsize subtracted 1 because want last index @hotellist = [] @hotellistsize = integer(response["hotellistresponse"]["hotellist"]["@size"]) -1 (0..(@hotellistsize)).each |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 simpler work?, like:
<input type="button" value="reload page" onclick="document.location.reload(true)";"$sort=price"> >
short part
you have several problems, solve main problem, try:
<%= button_to 'price', :controller => 'database', :action => 'getlist', :method => 'post', :params => {:sort => 'price'} %> <%= button_to 'quality', :controller => 'database', :action => 'getlist', :method => 'post', :params => {:sort => 'quality'} %> <%= button_to 'distance', :controller => 'database', :action => 'getlist', :method => 'post', :params => {:sort => 'proximity'} %>
in controller:
def getlist() $sort = ['price', 'quality', 'proximity'].find{|s| s == params[:sort]} $sort ||= 'price' # sort price default # code...
long part
after short part works.
Comments
Post a Comment