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属性。这似乎是拥有这种东西的自然场所。
添加回答
举报