2015-11-08 107 views
2

我需要在我正在处理的任务上使用嵌套窗体,并且因为我的嵌套窗体属性不会提交到数据库而卡住了。嵌套表单属性不会保存

这里是我的控制器是什么样子

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在轨道控制台,它什么都不返回。

我在做什么错?

+1

您能向我们展示从表单获得的发送参数吗? –

+0

请在提交请求时显示'params'和'booking_params'的值。 – tompave

+0

'开始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 ' –

回答

0

经过与sunnyk的配对会话后,我能够看到错误。

第一个错误是我的class Passengerbelongs_to :bookings而不是belongs_to :booking。尽管这是一个常见的错误。这些类之间的关联,现在看起来像:

class Booking < ActiveRecord::Base 
    belongs_to :flight 
    has_many :passengers 
    accepts_nested_attributes_for :passengers, reject_if: 
    lambda {|attributes| attributes['name'].blank?}, :allow_destroy => true 
end 

class Passenger < ActiveRecord::Base 
    belongs_to :booking 
end 

class Flight < ActiveRecord::Base 
    has_many :bookings 
    has_many :passengers, through: :bookings 
    accepts_nested_attributes_for :passengers 
    accepts_nested_attributes_for :bookings 
end 

下一页: 而不是使用的no_of_passengers默认值建立我的嵌套形式,我用了cocoon宝石,这使得嵌套的形式建设和管理更容易。我还编写了一个新的params方法,其中我允许flight_id,然后将其作为我的预订实例的参数在我的new方法中与我的current user一起传递。所以现在我的new方法看起来像这样。

def new 
    @booking = Booking.new(new_booking_params) 
    @booking.user = current_user if current_user 
end 

def new_booking_params 
    params.permit(:flight_id) 
end 

在那之后,我不得不做出另一个params方法我create方法,以便使我想在bookings表中的参数,这包括:passengers_attributes。现在我的create方法看起来像这样。

def create 
    @booking = Booking.new(another_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 

def another_booking_params 
    params.require(:booking).permit(:flight_id, :user_id, :no_of_passengers, 
    passengers_attributes:[:name, :email]) 
end 

最后,我不得不调整我的形式看起来像这样。

<%= form_for(@booking, url: bookings_path) do |f| %> 
    <%= f.hidden_field(:flight_id)%> 
    <%= f.hidden_field(:user_id) %> 
    <%= f.hidden_field(:no_of_passengers)%> 
    <%= f.fields_for :passengers do |passenger| %> 
    <%= render 'passenger_fields', :f => passenger %> 
    <% end %> 
    <%= link_to_add_association 'Add Another passenger',f, :passengers, :class => 'btn btn-primary add' %> 
    <%= submit_tag "Book Now", class: "btn btn-primary book" %> 
<% end %> 

passenger_fields部分看起来像。

<div class="nested-fields form-inline"> 
<div class="form-group"> 
    <%= f.text_field :name, :class => "form-control", placeholder: "Passenger Name" %> 
    </div> 
    <div class="form-group"> 
    <label>-</label> 
    <%= f.text_field :email, :class => "form-control", placeholder: "Passenger Email" %> 
    </div> 
    <div class="links pull-right"> 
    <%= link_to_remove_association "Delete", f, class: "btn btn-danger" %> 
    </div> 
    <hr> 
</div> 

所有的事情都做到了。我希望这可以帮助其他人更好地理解嵌套表单