Ruby on rails,新的 POST to API 和刷新页面

Ruby on rails, new POST to API and refresh page

本文关键字:API 刷新 to POST on rails 新的 Ruby      更新时间:2023-09-26

我是 Ruby on Rails 的新手。在我们的网站上,用户可以搜索,然后获得结果列表。

现在我想添加查看排序结果的功能(按价格、评级等)。API 进行排序,所以我所要做的就是使用新选项(即 &sort=PRICE)向 API 发送一个新的 post 请求,然后它将按顺序返回结果。

我的问题是我不知道该怎么做,我已经尝试了button_tagbutton_to,如果单击button_to然后手动刷新它可以工作,但是如果我添加多个button_to标签,它就不会。

我已经将$sort作为全局变量(在 API 文档中具有默认排序值,即 no_sort),因此当按下按钮时,我尝试将该变量更改为所需的名称然后发布,这就是我的观点:

<%= 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 %>

我的控制器 :

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

或者更简单的东西可以工作吗?,比如:

<input type="button" value="Reload Page" onClick="document.location.reload(true)";"$sort=PRICE"> >

短部分

您有几个问题,但要解决主要问题,请尝试:

<%= 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'} %>

在控制器中:

def getList()
  $sort = ['PRICE', 'QUALITY', 'PROXIMITY'].find{|s| s == params[:sort]}
  $sort ||= 'PRICE' # sort by price by default
  # your code...

长零件

经过短暂的部分工作。