0

I have a flights table and passengers table. When creating a new flight I need to be able to add multiple passengers to that flight.

I have managed to join my flights and passengers tables and the passengers are now showing up in the new flight form. The problem now is that I can't seem to save the passengers.

Flight form:

<label><%= form.label :passengers %></label>
    <%= collection_select(:passenger, :passenger_id, Passenger.all, :id, :first_name, {}, { :multiple => true } )%>

flight.rb

has_and_belongs_to_many :passengers

passenger.rb

has_and_belongs_to_many :flights

flights_controller.rb

def create
@flight = Flight.new(flight_params)

params[:passenger][:passenger_ids].each do |passenger_id|
  unless passenger_id.empty?
  passenger = Passenger.find(passenger_id)
    @flight.passengers << passenger
  end
end

respond_to do |format|
  if @flight.save
    format.html { redirect_to @flight, notice: 'Flight was successfully created.' }
    format.json { render :show, status: :created, location: @flight }
  else
    format.html { render :new }
    format.json { render json: @flight.errors, status: :unprocessable_entity }
  end
 end
end

schema.rb

create_table "flights", force: :cascade do |t|
 t.string "origin"
 t.string "destination"
 t.string "dep_time"
 t.string "arr_time"
 t.integer "seats"
 t.integer "price"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.integer "airline_id"
 t.index ["airline_id"], name: "index_flights_on_airline_id"
end


create_table "flights_passengers", id: false, force: :cascade do |t|
 t.integer "flight_id", null: false
 t.integer "passenger_id", null: false
 t.index ["flight_id", "passenger_id"], name: "index_flights_passengers_on_flight_id_and_passenger_id"
 t.index ["passenger_id", "flight_id"], name: "index_flights_passengers_on_passenger_id_and_flight_id"
end

create_table "passengers", force: :cascade do |t|
 t.string "first_name"
 t.string "last_name"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
end
Javier Menéndez Rizo
  • 2,138
  • 3
  • 12
  • 22
paul blackmore
  • 191
  • 2
  • 13

1 Answers1

0

You should add passengers to the flight before saving, like this:

@flight.passengers << passenger

You are adding it to the @boarding, can't see where you have declared that?

Alien
  • 258
  • 3
  • 10
  • I spotted that, sorry it was a typo. I have made the change now and it's still not saving. The error is "undefined method `each' for nil:NilClass". Any ideas – paul blackmore Jan 25 '19 at 14:33
  • Maybe collection_select is sending only one id, you can inspect that. Check your controller params, if you dont know how, call `byebug`(or whatever you have installed) in controller at the beginning of create action and call `params` in terminal. – Alien Jan 25 '19 at 14:55
  • Thanks for your help @Alien. I'm very new to Ruby on Rails, testing for bugs is something I know very little about. – paul blackmore Jan 25 '19 at 15:12
  • Np. It's not really hard to do it, you need to install gem, it's everything simple with Ruby on Rails, you can check next links: https://github.com/deivid-rodriguez/byebug and https://github.com/deivid-rodriguez/pry-byebug , no need for both just pick one. You will see there examples how to use it. – Alien Jan 25 '19 at 15:20
  • Thanks, I'll check them out now – paul blackmore Jan 25 '19 at 15:21
  • So it looks like the passengers selected are being sent but just not saving. Output below – paul blackmore Jan 25 '19 at 16:02
  • "✓", "authenticity_token"=>"kN7Mi2pm/nJJjncfThmIcQIDt2tJ19S5L6XWdcv2Z3TRB1CPBUswjoljkxEPchIxsAeaptI1WYUnxehZ5CI+jA==", "flight"=>"Berlin", "destination"=>"Palermo", "dep_time"=>"10:10", "arr_time"=>"12:30", "seats"=>"123", "price"=>"2345"} permitted: false>, "passenger"=>{"passenger_id"=>["", "1", "3", "4", "100"]}, "commit"=>"Create Flight", "controller"=>"flights", "action"=>"create"} permitted: false> – paul blackmore Jan 25 '19 at 16:02
  • It's saved in passenger_id not passenger_ids, try with `params[:passenger][:passenger_id].each` – Alien Jan 25 '19 at 16:07
  • Great spot. That has got me down a few more lines in create. The error I'm getting now is at this line @flight.passengers << passenger and the error is undefined local variable or method `passenger' for # Did you mean? @passenger passenger_id – paul blackmore Jan 25 '19 at 16:36
  • I have tried a few variations with the suggested change but nothing has worked yet. – paul blackmore Jan 25 '19 at 16:36
  • Try to exchange your code a little bit, first declare passenger then add to flight like `passenger = Passenger.find(passenger_id)` `@flight.passengers << passenger` remove end – Alien Jan 25 '19 at 16:52