我需要在我正在處理的任務上使用嵌套窗體,並且因爲我的嵌套窗體屬性不會提交到數據庫而卡住了。嵌套表單屬性不會保存
這裏是我的控制器是什麼樣子
def new
@booking = Booking.new
params[:no_of_passengers].to_i.times { @booking.passengers.build }
end
def create
@booking = Booking.new(booking_params)
respond_to do |format|
if @booking.save
format.html { redirect_to '/booking_confirmed', notice: 'Booking was successfully created.' }
format.json { render :show, status: :created, location: @booking }
else
format.html { render :new }
format.json { render json: @booking.errors, status: :unprocessable_entity }
end
end
end
private
def booking_params
params.permit(
:airline, :origin, :destination, :departure_date, :departure_time, :arrival_date,
:arrival_time, :flight_id, :price, :no_of_passengers, :user_id, :booking,
passenger_attributes: [
:id,:booking_id, :name, :email,:done,:_destroy
]
)
end
這裏是車型
class Booking < ActiveRecord::Base
has_many :passengers
accepts_nested_attributes_for :passengers, reject_if: lambda { |attributes| attributes['name'].blank? }
end
class Passenger < ActiveRecord::Base
belongs_to :bookings
end
這裏之間的關聯形式
<%= form_for @booking do |b| %>
<%= b.fields_for :passengers do |p| %>
<%= p.text_field :name, placeholder: "Passenger Name" %>
<%= p.text_field :email, placeholder: "Passenger Email" %>
<% end %>
<% end %>
我使用檢查乘客表Passenger.all
在軌道控制檯,它什麼都不返回。
我在做什麼錯?
您能向我們展示從表單獲得的發送參數嗎? –
請在提交請求時顯示'params'和'booking_params'的值。 – tompave
'開始POST「/預訂」爲127.0.0。1在2015年11月8日14時00分四十九秒0100 處理由BookingsController#創建以HTML 參數:{ 「UTF8」=> 「✓」, 「authenticity_token」=>「S95Splfs70P954xJ/AN8t9k8E8OBvwCRdwrx5nSSwziLo/GFF4I102/cbB3ZUDToz/oofuwZCQIr9tEmsZ + W4w ==「,」booking「=> {」flight_id「=>」608「,」user_id「=>」「,」no_of_passengers「=>」「,」passengers_attributes「=> {」1446987645041「=> {」名稱「=>」ade「,」email「=>」ade「,」_destroy「=>」false「}}},」commit「=>」立即預訂「} 未經許可的參數:passengers_attributes ' –