未找到HotelsController#rooms_availability的模板,渲染头:no_content

问题描述 投票:0回答:1

[我正在尝试通过下拉菜单提供可提供的房间,该菜单基于该表单中已填写的到达和离开信息。我正在尝试通过控制器操作rooms_availability实现此目标。

不幸的是我收到错误消息

Processing by HotelsController#rooms_availability as JS
  Parameters: {"arrival"=>"2019-11-26", "departure"=>"2019-11-27", "id"=>"22"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ /Users/username/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
  Hotel Load (0.2ms)  SELECT  "hotels".* FROM "hotels" WHERE "hotels"."id" = $1 LIMIT $2  [["id", 22], ["LIMIT", 1]]
  ↳ app/controllers/hotels_controller.rb:122
  CACHE Hotel Load (0.0ms)  SELECT  "hotels".* FROM "hotels" WHERE "hotels"."id" = $1 LIMIT $2  [["id", 22], ["LIMIT", 1]]
  ↳ app/controllers/hotels_controller.rb:103
  RoomCategory Load (0.3ms)  SELECT "room_categories".* FROM "room_categories" WHERE "room_categories"."hotel_id" = $1  [["hotel_id", 22]]
  ↳ app/controllers/hotels_controller.rb:103
  Room Load (0.3ms)  SELECT "rooms".* FROM "rooms" WHERE "rooms"."room_category_id" IN ($1, $2)  [["room_category_id", 4], ["room_category_id", 9]]
  ↳ app/controllers/hotels_controller.rb:103
  Room Load (0.4ms)  SELECT "rooms".* FROM "rooms" WHERE "rooms"."room_category_id" IN (SELECT "room_categories"."id" FROM "room_categories" WHERE "room_categories"."hotel_id" = $1)  [["hotel_id", 22]]
  ↳ app/controllers/hotels_controller.rb:113
  Room Load (0.5ms)  SELECT DISTINCT "rooms".* FROM "rooms" INNER JOIN "reservations" ON "reservations"."room_id" = "rooms"."id" WHERE "reservations"."hotel_id" = $1 AND (reservations.arrival <= '2019-11-26' AND '2019-11-27' >= reservations.departure)  [["hotel_id", 22]]
  ↳ app/controllers/hotels_controller.rb:113
No template found for HotelsController#rooms_availability, rendering head :no_content
Completed 204 No Content in 406ms (ActiveRecord: 2.1ms)

代码

reservations_controller

def new
  @hotel = Hotel.find(params[:hotel_id])
  @reservation = @hotel.reservations.new
  @room_categories = @hotel.room_categories
  @rooms = Room.where(room_category: @room_categories)
  authorize @reservation
end

reservations / new.html.erb

<%= simple_form_for [@hotel, @reservation] do |f|%>
  <div class="col col-sm-3">
    <%= f.input :arrival,
    as: :string,
    label:false,
    placeholder: "From",
    wrapper_html: { class: "inline_field_wrapper" },
    input_html:{ id: "start_date"} %>
  </div>
  <div class="col col-sm-3">
    <%= f.input :departure,
    as: :string,
    label:false,
    placeholder: "From",
    wrapper_html: { class: "inline_field_wrapper" },
    input_html:{ id: "end_date"} %>
  </div>

  <div class="col col-sm-4">
    <%= f.input :room_id, collection: @rooms, as: :grouped_select, group_by: proc { |room| room.room_category.name },  label:false %>
    <%#= f.input :room_id, collection: @room_categories.order(:name), as: :grouped_select, group_method: :rooms,  label:false %>
  </div>

  <%= f.button :submit, "Search", class: "create-reservation-btn"%>
<% end %>

预订脚本/new.html.erb

<script>
const checkIn = document.querySelector('#start_date');
const checkOut = document.querySelector('#end_date');
const checkInAndOut = [checkIn, checkOut];

checkInAndOut.forEach((item) => {
  item.addEventListener('change', (event) => {
    checkAvailability();
  })
})

  function checkAvailability(){

    $.ajax({
      url: "<%= rooms_availability_hotel_path(@hotel) %>" ,
      dataType: 'json',
      type: "POST",
      data: `arrival=${start_date.value}&departure=${end_date.value}`,
      success: function(data) {
        console.log('succes')
        console.log(data);
      },
      error: function(response) {
        console.log('failure')
        console.log(response);
      }
    });
  };
</script>

hotels_controller

def rooms_availability
  hotel = Hotel.includes(:rooms).find(params[:id])
  arrival = Date.parse room_params[:arrival]
  departure = Date.parse room_params[:departure]
  time_span = arrival..departure
  @unavailable_rooms = Room.joins(:reservations).where(reservations: {hotel: hotel}).where("reservations.arrival <= ? AND ? >= reservations.departure", arrival, departure).distinct
  hotel_cats = hotel.room_categories
  hotel_rooms = Room.where(room_category: hotel_cats)
  @rooms = hotel_rooms - @unavailable_rooms
  respond_to do |format|
    format.js
  end
end

def room_params
  params.permit(:arrival, :departure, :format, :id)
end

型号

class Reservation < ApplicationRecord
  belongs_to :hotel
  belongs_to :room
end

class Hotel < ApplicationRecord
  has_many :room_categories, dependent: :destroy
  has_many :rooms, through: :room_categories
  has_many :reservations, dependent: :destroy
end

class RoomCategory < ApplicationRecord
  belongs_to :hotel
  has_many :rooms, dependent: :destroy
  accepts_nested_attributes_for :rooms, allow_destroy: true
end

class Room < ApplicationRecord
  belongs_to :room_category
  validates :name, presence: true
  has_many :reservations, dependent: :destroy
  accepts_nested_attributes_for :room_category
end
javascript ruby-on-rails activerecord
1个回答
0
投票

您需要一个rooms_availability.js.erb文件。它正在尝试渲染它,但找不到它。这与发生新操作并尝试在没有new.html.erb的情况下导航到新视图时类似。

在js局部文件中,您可以定位div或其他内容,并使用escape_javascript或某些内容通过它呈现局部文件

© www.soinside.com 2019 - 2024. All rights reserved.