简体   繁体   中英

Access nested params in Rails - attributes from checkbox

I'm trying to access the users array that I get from a check box, to add to an HABTM association between a Reservation and User model.

The params hash is the following:

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"}

My method code is:

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

My helpers are defined as:

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

The way I'm currently accessing the users part of params hash gives me empty variables... How am I supposed to access those values?

您可以简单地以params[:reservation][:users]访问它们,无需为此使用强参数,因为它们没有在批量分配中使用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM