访问 Rails 中的嵌套参数 - 来自复选框的属性
Access nested params in Rails - attributes from checkbox
我正在尝试访问从复选框中获取的用户数组,以添加到预订和用户模型之间的 HABTM 关联。
参数散列如下:
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 哈希的用户部分的方式给我空变量...我应该如何访问这些值?
您可以像 params[:reservation][:users]
一样访问它们,不需要为此使用强参数,因为它们不用于批量分配。
我正在尝试访问从复选框中获取的用户数组,以添加到预订和用户模型之间的 HABTM 关联。
参数散列如下:
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 哈希的用户部分的方式给我空变量...我应该如何访问这些值?
您可以像 params[:reservation][:users]
一样访问它们,不需要为此使用强参数,因为它们不用于批量分配。