对于房间预订,我想提供房间选项。
选项及其连接表reservation_options似乎已正确插入到我的参数中,但我无法正确分配连接表的option_quantity值。它给了我错误消息``没有将符号隐式转换为整数``
由于选项是在表单中动态生成的,因为它们取决于所选的 room_type,因此我尝试在创建操作中构建它们。但是,我无法迭代参数并构建它们(请参阅我的尝试的创建操作)。
参数
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
预订 Controller
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
reservation_option 的输出 1
=> <ActionController::Parameters {"option_id"=>"110", "option_quantity"=>"1"} permitted: false>
reservation_option 的输出 2
=> #<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">]>
最佳答案
我认为这可能会满足您的要求:
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 isn't own_to :hotel
,而您当前未在 Reservation
模型中显示它。而且,酒店有很多:预订
。
这还假设您的 ReservationOption
模型具有 option_quantity
属性。这似乎是拥有这样的东西的自然场所。
关于javascript - 如何在create action中创建JOIN表并分配相关id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58553713/