我正在尝试访问我从复选框中获取的用户数组,以添加到预订和用户模型之间的HABTM关联。
params散列如下:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"vx0dTg8Q2XpyFcXpmmiwbwtFwNiRO25CQImGBasArDZjEbOmjh2lkBSdQGl8Z2Sgr7+kF2qveergEFjZr63AHQ==", "reservation"=>{"name"=>"daads", "phone"=>"", "email"=>"", "number_people"=>"1", "duration"=>"1", "address"=>"", "description"=>"", "users"=>["", "3", "2"]}, "commit"=>"Create Reservation", "date"=>"2016-10-30", "hour"=>"01:30", "id"=>"1"}
我的方法代码是:
def add_reservation
@business = Business.find(params[:id])
@reservation = Reservation.new(reservation_params)
init_date = params[:date].to_s + " " + params[:hour].to_s + ":00"
@reservation.date = init_date.to_datetime
@reservation.date_end = (@reservation.date.to_time + @reservation.duration.hours).to_datetime
user_reservation_params[:users].each do |u|
@reservation.users << u
end
@business.reservations << @reservation
flash[:success] = "Reservation added!"
redirect_to business_path(@business)
end
我的助手定义为:
private
def reservation_params
params.require(:reservation).permit(:name, :email, :address, :phone, :duration, :date, :date_end, :number_people, :description)
end
private
def user_reservation_params
params.require(:reservation).permit(:users => [])
end
我当前访问params hash的用户部分的方式给了我空变量...我该如何访问这些值?
答案 0 :(得分:0)
您可以简单地以params[:reservation][:users]
的形式访问它们,不需要使用强参数,因为它们不用于批量分配。