参数不保存或辅助

时间:2016-11-22 04:28:43

标签: ruby-on-rails ruby-on-rails-4

我无法在显示页面页面显示信息。我认为是因为flight_id正以零保存到数据库中。也许该协会没有建立或我的形式可能是错的。我不确切知道,需要帮助搞清楚。我已粘贴下面的模型,视图和控制器。任何关于我做错事的见解都会很棒

预订控制器

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"=>"fasdfasd@afda.com"}}}, "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", "fasdfasd@afda.com"], ["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)

2 个答案:

答案 0 :(得分:0)

看看这个Examples of new, create actions for has_many :through associations (nested)

使用嵌套属性创建航班预订。

所以new

中的BookingsController行动

必须

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将被保存以供预订。

对于表单,您可以按照链接中提供的说明进行操作。

答案 1 :(得分:0)

这两个字段

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

未包含在booking_params中。要解决此问题,您可以在new操作

中添加它们
def new
  @booking = Booking.new(:flight_id => params[:flight_id], :passenger => params[:passenger])

  ...
end

然后在表单中,执行此操作

= f.hidden_field :flight_id
= f.hidden_field :passenger