1 回答

TA貢獻1773條經驗 獲得超3個贊
我認為這可能會滿足您的要求:
class ReservationsController < ApplicationController
def create
@user = current_user
@hotel = Hotel.find(params[:hotel_id])
@reservation = @hotel.reservations.new(reservation_params)
if @reservation.save
params[:reservation_options_attributes].each do |reservation_option|
if @option = Option.find_by(id: reservation_option[:option_id])
@reservation.options << @option
reservation_option = @reservation.reservation_options.where(option: @option)
reservation_option.update(option_quantity: reservation_option[:option_quantity])
end
end
authorize @reservation
redirect_to hotel_path(@hotel)
else
@room_type_list = @hotel.room_types
render 'new'
end
end
end
自然而然地,這假定Reservation belongs_to :hotel您當前未在Reservation模型中顯示的。還有,那個Hotel has_many :reservations。
這也假設您的ReservationOption模型具有option_quantity屬性。這似乎是擁有這種東西的自然場所。
添加回答
舉報