Ruby on Rails帮助创建form_tag来自hotelbeds Api的酒店可用性

时间:2016-05-27 18:20:35

标签: ruby-on-rails ruby-on-rails-4 httparty rails-api

我正努力让我的表单从hotelbeds api获得特定酒店的可用性,我没有使用任何宝石。

hotelbeds文档:

hotelCode =>整数

checkIn =>串

checkout =>串

占用率=>字符串

占有率。房间〜成人〜儿童型年龄。实例:1~1~1~AD-30; CH-8,2~1~0~AD-30

hotelbeds的示例URI:

https://api.test.hotelbeds.com/hotel-api/1.0/hotels/6914?checkIn=2016-06-15&checkOut=2016-06-20&occupancies=1%7E1%7E0%7EAD-30

我很高兴得到任何帮助。

这是我的HotelsController行动:

def search_hotel
  hotel_id    = params["hotel_id"]
  check_in    = params["check_in"]
  check_out   = params["check_out"]
  occupancies = params["occupancies"]
  rooms       = params["rooms"]
  adults      = params["adults"]
  children    = params["children"]

  @hotels = Hotel.search_hotel(check_in, check_out, occupancies, hotel_id)
  logger.debug "#"*120
  logger.debug @hotels.inspect
  logger.debug "#"*120

def  hotel_params
  params.require(:hotel).permit(:hotel_id, :check_in, :check_out, :occupancies, :rooms, :adults, :children)
end

我的表格:

<%= form_tag search_hotel_path, method: :get do %>
<div class="form-group">
  <%= label_tag :hotel_id %> 
  <%= text_field_tag :hotel_id, nil %>
</div>

<div class="form-group">
<%= label_tag :check_in %> 
<%= date_field_tag :check_in, Date.today %>
</div>
<div class="form-group">
<%= label_tag :check_out %> 
<%= date_field_tag :check_out, Date.today + 2 %>
</div>

<%#= label_tag :occupancies %> 
<%#= text_field_tag :occupancies, nil %>
<div class="form-group">
  <%= fields_for :occupancies do |f| %>
    <%= f.label :rooms %>
    <%= f.select :rooms,  options_for_select([ "1", "2", "3", "4" ]) %>

    <%= f.label :adults %> 
    <%= f.select :adults,  options_for_select([ "1", "2", "3", "4" ]) %>

    <%= f.label :children %> 
    <%= f.select :children,  options_for_select([ "0", "1", "2", "3", "4" ]) %>
  <% end %>
</div>
<%= submit_tag 'Search Hotel' %>
  

我的酒店型号:

  def self.search_hotel(check_in, check_out, hotel_id, occupancies)


# occupancies = "#{rooms}~#{adults}~#{children}"
# occupancies = ""
# occupancies << rooms
# occupancies << '~'
# occupancies << adults #adults.to_s.rjust(2, '0')
# occupancies << '~'
# if children != '0'
#   occupancies << children
# end
# # type-age
# occupancies << ' AD-30'

occupancies = "1~1~0~AD-30"


url = "https://api.test.hotelbeds.com/hotel-api/1.0/hotels/"
response = HTTParty.get(url,
  :query => { "hotelCode" => hotel_id, "checkIn" => check_in, "checkOut" => check_out, "occupancies" => occupancies },
  :headers => add_signature_for_hotels
)
puts "-"*120
puts response.body
puts "-"*120
JSON.parse(response.body)["hotels"]

0 个答案:

没有答案