Для бронирования номера я бы хотел включить варианты для номера.
Опции и их таблица соединений booking_options правильно вставлены в мои параметры, но я не могу правильно назначить значение option_quantity таблицы соединений. Это дает мне сообщение об ошибке `` `неявного преобразования Symbol в целое число` `
Поскольку параметры динамически генерируются в форме, поскольку они зависят от выбранного room_type, я попытался создать их в действии create,Однако я не могу перебрать параметры и построить их (см. «Создание действия для моей попытки»).
Параметры
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7QJHgdYW8ubKiNpLPET7tYzGNyqmp69dafo8NDaAUBf2PL3CI9XhKmd/am2Mo/A93EFZQByltwe2e4wepMXdqw==", "reservation"=>{"rooms"=>{"room_type"=>"185"}, "arrival"=>"2019-10-25", "departure"=>"2019-10-26", "room_id"=>"266", "reservation_contact_attributes"=>{"first_name"=>"John", "last_name"=>"Doe", "street"=>"street", "street_number"=>"", "zipcode"=>"4049", "city"=>"Gent", "country"=>"", "email"=>"john@hotmail.com", "phone"=>""}, "payment"=>"not paid"}, "reservation_options_attributes"=>[{"option_id"=>"109", "option_quantity"=>"1"}, {"option_id"=>"110", "option_quantity"=>"1"}], "commit"=>"Save & proceed to additional options", "hotel_id"=>"109"}
Модели
class Reservation < ApplicationRecord
has_many :reservation_options, dependent: :destroy
has_many :options, through: :reservation_options
accepts_nested_attributes_for :reservation_options
end
class ReservationOption < ApplicationRecord
belongs_to :option
belongs_to :reservation
accepts_nested_attributes_for :option
end
class Option < ApplicationRecord
belongs_to :room_type
has_many :reservation_options, dependent: :destroy
has_many :reservations, through: :reservation_options
validates :name, presence: true
end
Контроллер резервирования
class ReservationsController < ApplicationController
# skip_before_action :authenticate_user!
def new
@user = current_user
@hotel = Hotel.find(params[:hotel_id])
@reservation = Reservation.new
@age_tables = @hotel.age_tables
@reservation.build_reservation_contact
@room_type_list = @hotel.room_types
@all_options = @hotel.options
@rooms = []
@options = []
@extra_guests = []
if params[:room_type].present?
@rooms = Room.find(params[:room_type]).rooms
@options = Room.find(params[:room_type]).options
@extra_guests = Room.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}
}
format.js
end
end
authorize @reservation
end
def create
def create
@user = current_user
@hotel = Hotel.find(params[:hotel_id])
@reservation = Reservation.new(reservation_params)
@reservation.hotel = @hotel
authorize @reservation
if @reservation.save
params[:reservation_options_attributes].each do |reservation_option|
if @option = Option.find_by(id: reservation_option[:option_id])
@reservation.options << @option
# output 1 for reservation_option
reservation_option = @reservation.reservation_options.where(option: @option)
# output 2 for reservation_option
reservation_option.update(option_quantity: reservation_option[:option_quantity])
# Error message
end
end
redirect_to root_path
else
@room_type_list = @hotel.room_types
render 'new'
end
end
private
def reservation_params
params.require(:reservation).permit(:room_id, :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
end
вывод 1 для booking_option
=> <ActionController::Parameters {"option_id"=>"110", "option_quantity"=>"1"} permitted: false>
вывод 2 для booking_option
=> #<ActiveRecord::AssociationRelation [#<ReservationOption id: 62, option_id: 110, reservation_id: 142, option_quantity: nil, created_at: "2019-10-25 12:27:45", updated_at: "2019-10-25 12:27:45">]>