首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails 4参数丢失或值为空: booking

rails 4参数丢失或值为空: booking
EN

Stack Overflow用户
提问于 2016-11-21 01:56:37
回答 1查看 70关注 0票数 0

我已经知道如何修复这个错误,参数缺失或者值为空: booking。我所有的寻找都还没有得到答案。据我所知,booking对象返回nil,我不知道如何修复它。任何帮助都将不胜感激。

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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'
EN

回答 1

Stack Overflow用户

发布于 2016-11-21 03:33:15

控制器中的create操作等待params结构将如下所示:

代码语言:javascript
复制
{ booking: {flight_id: id, passenger_num: num, passengers_attributes: attrs }}

但您发送时没有在根目录中输入"booking“键。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40707402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档