В форме бронирования пользователь может заполнить room_type. Следовательно, я хотел бы перебрать все опции, относящиеся к этому типу комнаты, и позволить людям назначить количество для этой опции.
Текущий статус
Я могу получить всепараметры, основанные на пользовательском вводе через JQuery, но я не знаю, как сгенерировать поле simple_form для каждого из них, где они могут заполнить количество.
Текущий вывод в консоли
{rooms: Array(4), options: Array(1), extra_guests: Array(1)}
rooms: (4) [{…}, {…}, {…}, {…}]
extra_guests: [{…}]
options: Array(1)
0: {id: 109, accommodation_category_id: 185, name: "Amazing option", description: "", rank: null, …}
length: 1
__proto__: Array(0)
__proto__: Object
код форма
<%= simple_form_for [@hotel, @reservation] do |f|%>
<%= f.simple_fields_for :rooms do |room| %>
<%= room.input :room_type, collection: @room_type_list, input_html:{
id: "room_type"
}%>
<% end %>
<h4>Options</h4>
<!-- List all options for room_type by name and display field for option_quantity -->
<% end %>
<script >
// dynamic options for change category
$(document).on("change", "#room_type", function(){
var room_type = $(this).val();
$.ajax({
url: "/hotels/<%= @hotel.id %>/reservations/new",
method: "GET",
dataType: "json",
data: {room_type: room_type},
error: function (xhr, status, error) {
console.error('AJAX Error: ' + status + error);
},
success: function (response) {
console.log(response);
var options = response["options"];
// Code to generate list of options
}
});
});
</script>
контроллер бронирования
class ReservationsController < ApplicationController
# skip_before_action :authenticate_user!
def new
@hotel = Hotel.find(params[:hotel_id])
@reservation = Reservation.new
@room_type_list = @hotel.room_types
@all_options = @hotel.options
@rooms = []
@options = []
@extra_guests = []
# # Display rooms/options for category
if params[:room_type].present?
@rooms = RoomType.find(params[:room_type]).rooms
@options = RoomType.find(params[:room_type]).options
@extra_guests = RoomType.find(params[:room_type]).extra_guests
end
if request.xhr?
respond_to do |format|
format.json {
render json: {rooms: @rooms, options: @options, extra_guests: @extra_guests}
}
end
end
authorize @reservation
end
private
def reservation_params
params.require(:reservation).permit(:arrival, :departure, :payment, :reservation_contact_id, option_ids:[],
reservation_contact_attributes: [:id, :first_name,
:last_name, :first_name, :last_name, :zipcode, :city, :street, :street_number,
:email, :phone, :date_of_birth, :country, :company, :gender, :vat_number],
rooms_attributes: [:id,:name, :room_type_id,
room_types_attributes: [:id, :name]],
reservation_options_attributes: [:id, :option_id, :option_quantity, :_destroy,
options_attributes: [:id, :name, :room_type_id, :description,
room_types_attributes:[:id, :name]]],
reservation_extra_guests_attributes: [:id, :extra_guest_id, :extra_guest_quantity, :_destroy,
extra_guests_attributes: [:id, :name, :room_type_id, :age_table_id,
room_types_attributes:[:id, :name]]])
end
модели
class Reservation < ApplicationRecord
belongs_to :hotel
belongs_to :room
has_many :reservation_options, inverse_of: :reservation, dependent: :destroy
accepts_nested_attributes_for :reservation_options
has_many :options, through: :reservation_options
end
class Room < ApplicationRecord
belongs_to :room_type
validates :name, presence: true
has_many :reservations, dependent: :destroy
accepts_nested_attributes_for :room_type
end
class RoomType < ApplicationRecord
belongs_to :hotel
has_many :rooms, dependent: :destroy
accepts_nested_attributes_for :rooms, allow_destroy: true
has_many :options, dependent: :destroy
accepts_nested_attributes_for :options, allow_destroy: true
end
class Option < ApplicationRecord
belongs_to :room_type
has_many :reservation_options, dependent: :destroy
has_many :option_prices, dependent: :destroy, inverse_of: :option
end