2015-11-08 112 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> 

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