我需要在我正在处理的任务上使用嵌套窗体,并且因为我的嵌套窗体属性不会提交到数据库而卡住了。嵌套表单属性不会保存
这里是我的控制器是什么样子
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 ' –