Ruby on rails 无符号到整数的隐式转换:如何在创建操作中插入联接表值
对于房间预订,我想包括房间的选项 由于选项是在表单中动态生成的,因为它们依赖于选定的房间类型,因此我尝试在创建操作中构建它们 选项及其联接表保留选项似乎已正确插入到我的参数中,但我无法正确分配联接表的选项数量值。它给我错误消息Ruby on rails 无符号到整数的隐式转换:如何在创建操作中插入联接表值,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,对于房间预订,我想包括房间的选项 由于选项是在表单中动态生成的,因为它们依赖于选定的房间类型,因此我尝试在创建操作中构建它们 选项及其联接表保留选项似乎已正确插入到我的参数中,但我无法正确分配联接表的选项数量值。它给我错误消息没有将符号隐式转换为整数 参数 "authenticity_token"=>"o4KMn/lP9tYwpDhZTsBFEYHLfwWVanYYZ6bdrkK84QO4vHbcDIzlGp1TiH/+J06Z0UwRby9obkK4J22E0Plsvw==", "r
没有将符号隐式转换为整数
参数
"authenticity_token"=>"o4KMn/lP9tYwpDhZTsBFEYHLfwWVanYYZ6bdrkK84QO4vHbcDIzlGp1TiH/+J06Z0UwRby9obkK4J22E0Plsvw==",
"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"=>"Amstreet", "street_number"=>"", "zipcode"=>"4049", "city"=>"Gent", "country"=>"", "email"=>"john@hotmail.com", "phone"=>""},
"payment"=>"paid"},
"reservation_options_attributes"=>[{"option_id"=>"110", "option_quantity"=>"1"}, {"option_id"=>"109", "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 Reservation
预订控制器
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
def创建
@用户=当前用户
@hotel=hotel.find(参数[:hotel_id])
@保留=保留。新建(保留参数)
@预订.酒店=@酒店
授权@预订
如果@reservation.save
参数[:保留|选项|属性]。每个do |保留|选项|
如果@option=option.find\u by(id:reservation\u option[:option\u id])
@reservation.options“110”,“option_quantity”=>“1”}允许:false>
保留选项的输出2
=> <ActionController::Parameters {"option_id"=>"110", "option_quantity"=>"1"} permitted: false>
=> #<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">]>
=>#
这些关系在模型中是什么样子的?这是不是一个比你有很多:通过?的回应。我添加了相关的模型,是的,它有一个有很多:通过对,所以你不需要推到revervation的选项。如果这是你的问题的话,那么这个选项的数量确实是在保留选项上?这仍然是太多不必要的工作了。您有接受\u嵌套的\u属性\u用于:预订\u选项
。可以简单地将连接对象放在params[:reservation][:reservation\u options\u attributes]
中,在reservation\u params
中允许这样做,就这样!现在Reservation.create(Reservation\u参数)
将创建所有内容。