rails 4 param缺失或值为空:预订

时间:2016-11-20 17:56:37

标签: ruby-on-rails ruby-on-rails-4.2

我是如何修复此错误参数丢失或值为空:预订。 我的所有搜索还没有得到回答。据我所知,预订对象返回零,我不知道如何解决它。任何帮助将不胜感激。

class BookingsController < ApplicationController  
   skip_before_action :verify_authenticity_token, :only => :create

def new
    @booking = Booking.new

    @flight = Flight.find(params[:flight_id])
    @passengers = params[:passenger_num].to_i
    @passengers.to_i.times { @booking.passengers.build }
end

def create
    @booking = Booking.new(booking_params)

    if @booking.save
        flash[:success] = 'Your flight has been booked'
        redirect_to @booking
    else
        flash[:danger] = 'flight booking failure!'
        render 'new'
    end
  end

  def show
     @booking = Booking.find(params[:id])
  end

  private

    def booking_params
        params.require(:booking).permit(:flight_id,         :passenger_num,          passengers_attributes: [:name, :email])
  end
end

当我提交嵌套表单时,问题发生在创建操作中。

ActionController::ParameterMissing in BookingsController#create
param is missing or the value is empty: booking


.row
.col-md-6.col-md-offset-3
    table.table
        tr
            th Flight Number
            th Departure
            th Destination
            th Date
            th Passengers
        tbody
            tr
                td = @flight.id
                td = @flight.from_airport.code
                td = @flight.to_airport.code
                td = @flight.date
                td = @passengers
    = form_for @booking do |f|  
        - @passengers.times do 
            = f.fields_for :passenger_num do |p|
                = p.label :name
                = p.text_field :name, class: 'form-control'

                = p.label :email
                = p.text_field :email, class: 'form-control'

            = f.hidden_field :flight_id, value: params[:flight_id]
            = f.hidden_field :passenger_num, value: params[:passenger_num]
            = hidden_field_tag :flight_id, params[:flight_id]
            = hidden_field_tag :passenger_num, params[:passenger_num]

        = f.submit 'Finish', class: 'btn btn-primary btn-block'

1 个答案:

答案 0 :(得分:0)

您在控制器中的创建操作会等待params结构如下:

{ booking: {flight_id: id, passenger_num: num, passengers_attributes: attrs }}

但是您在根目录中没有“预订”键发送。