2016-11-22 37 views
0

私のshow pageページに情報を表示するのに問題があります。flight_idがデータベースにnilとして保存されているためです。多分、協会はビルドしていないか、私のフォームが間違っている可能性があります。私は正確にわからないし、それを理解するのに助けが必要です。モデル、ビュー、コントローラを以下に貼り付けました。私が間違っているの何についてどのような洞察力はパラメータが保存または設定されていません

予約コントローラ

class BookingsController < ApplicationController 
    def new 
    @booking = Booking.new 

    @flight = Flight.find(params[:flight_id]) 
    @passenger = params[:passenger].to_i 
    @passenger.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.fetch(:booking).permit(:flight_id, passengers_attributes: [:name, :email]) 
     end 
end 

new.html.slim

.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 = @passenger 
     = form_for @booking do |f| 
      = f.fields_for :passengers do |p| 
       = p.label :name 
       = p.text_field :name, class: 'form-control' 

       = p.label :email 
       = p.text_field :email, class: 'form-control' 

       = hidden_field_tag :flight_id, params[:flight_id] 
       = hidden_field_tag :passenger, params[:passenger] 

      = f.submit 'Finish', class: 'btn btn-primary btn-block' 

乗客モデル

class Passenger < ActiveRecord::Base 
    has_many :flights, through: :bookings 
    has_many :bookings, through: :passenger_bookings 
    has_many :passenger_bookings 
end 

フライトモデル

素晴らしいことです
class Flight < ActiveRecord::Base 
    belongs_to :from_airport, foreign_key: :start_airport_id, class_name: 'Airport' 
    belongs_to :to_airport, foreign_key: :end_airport_id, class_name: 'Airport' 

    has_many :passengers 
    has_many :bookings 

    def self.all_airports 
    Airport.all 
    end 

    def self.all_dates 
     Flight.pluck(:date).uniq.sort 
    end 

    def self.search(start, finish, date) 
    Flight.where(start_airport_id: start, 
          end_airport_id: finish, 
          date: date) 
    end 
end 

予約モデル

class Booking < ActiveRecord::Base 
    belongs_to :flight 
    has_many :passengers, through: :passenger_bookings 
    has_many :passenger_bookings 
    accepts_nested_attributes_for :passengers 
end 

スキーマ

ActiveRecord::Schema.define(version: 20161122010622) do 

    create_table "airports", force: :cascade do |t| 
    t.string "code" 
    t.string "city" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "bookings", force: :cascade do |t| 
    t.integer "flight_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "flights", force: :cascade do |t| 
    t.integer "start_airport_id" 
    t.integer "end_airport_id" 
    t.integer "duration" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.date  "date" 
    end 

    add_index "flights", ["end_airport_id"], name: "index_flights_on_end_airport_id" 
    add_index "flights", ["start_airport_id"], name: "index_flights_on_start_airport_id" 

    create_table "passenger_bookings", force: :cascade do |t| 
    t.integer "passenger_id" 
    t.integer "booking_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    add_index "passenger_bookings", ["booking_id"], name: "index_passenger_bookings_on_booking_id" 
    add_index "passenger_bookings", ["passenger_id"], name: "index_passenger_bookings_on_passenger_id" 

    create_table "passengers", force: :cascade do |t| 
    t.string "name" 
    t.string "email" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

end 

show.html.slim

h1 = @booking.id 
h2 = @booking.flight_id 
h2 = @booking.flight.from_airport.code 
h2 = @booking.flight.to_airport.code 
h2 = @booking.passengers 

サーバログ

Started POST "/bookings" for 127.0.0.1 at 2016-11-21 20:56:58 -0500 
Processing by BookingsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"rVU9PFoNQSoeFZDzximvSTFU1L88RynjNxLpL7rKaBvJhWP9QCIkqxzvRTPzR/7yp7MTdnU9ZZnf1/WkJwu84w==", "booking"=>{"passengers_attributes"=>{"0"=>{"name"=>"mighty", "email"=>"[email protected]"}}}, "flight_id"=>"5", "passenger"=>"1", "commit"=>"Finish"} 
    (0.5ms) begin transaction 
    SQL (0.9ms) INSERT INTO "bookings" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2016-11-22 01:56:58.288407"], ["updated_at", "2016-11-22 01:56:58.288407"]] 
    SQL (23.1ms) INSERT INTO "passengers" ("name", "email", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "mighty"], ["email", "[email protected]"], ["created_at", "2016-11-22 01:56:58.297067"], ["updated_at", "2016-11-22 01:56:58.297067"]] 
    SQL (1.0ms) INSERT INTO "passenger_bookings" ("booking_id", "passenger_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["booking_id", 41], ["passenger_id", 46], ["created_at", "2016-11-22 01:56:58.340140"], ["updated_at", "2016-11-22 01:56:58.340140"]] 
    (310.9ms) commit transaction 
Redirected to http://localhost:3000/bookings/41 
Completed 302 Found in 400ms (ActiveRecord: 336.4ms) 


Started GET "/bookings/41" for 127.0.0.1 at 2016-11-21 20:56:58 -0500 
Processing by BookingsController#show as HTML 
    Parameters: {"id"=>"41"} 
    Booking Load (0.4ms) SELECT "bookings".* FROM "bookings" WHERE "bookings"."id" = ? LIMIT 1 [["id", 41]] 
    Rendered bookings/show.html.slim within layouts/application (0.5ms) 
Completed 200 OK in 375ms (Views: 371.2ms | ActiveRecord: 0.4ms) 

答えて

0

ハこれを見てくださいExamples of new, create actions for has_many :through associations (nested)

フライトの予約を作成するためにネストされた属性を使用します。

のでBookingsController

でのごnewアクションで

def new 
@flight = Flight.find(params[:flight_id]) 
@booking = @flight.bookings.build 
@passenger = params[:passenger].to_i 
@passenger.to_i.times { @booking.passengers.build } 
end 

今飛行idは予約のために保存されますでなければなりません。

フォームの場合、リンクに記載されている指示に従うことができます。

0

これらの二つのフィールド

= hidden_field_tag :flight_id, params[:flight_id] 
= hidden_field_tag :passenger, params[:passenger] 

booking_paramsには含まれていません。これを修正するには、

def new 
    @booking = Booking.new(:flight_id => params[:flight_id], :passenger => params[:passenger]) 

    ... 
end 

newアクションでそれらを追加することができますし、フォームに、この

= f.hidden_field :flight_id 
= f.hidden_field :passenger 
を行います
関連する問題