In this project, you’ll get a chance to tackle some more advanced forms. This is the kind of thing you’ll have to work with when handling user orders for anything more complicated than an e-book. In this case, we’ll build the first three steps of a typical checkout process for booking a one-way flight:
A typical airline booking flow:
Step 4 would be done via integration of something like Paypal, via a gem or an SDK or Stripe.
We’ll be creating a one-way flight booker. You’ll get lots of practice populating and building dropdown menus, radio buttons, and nested submissions. Let the fun begin!
This project will require you to seed your database, so use your db/seeds.rb
file to write the code necessary to populate Airports and Flights. One trick for toy apps like this (don’t do it for production!) is to make your seeds file ::delete_all
items from each table in your database and then completely repopulate them. That way, when you create a new model or change how you want your sample data set up, you can just update the seeds.rb
file and rerun $ rake db:seed
.
odin-flight-booker
, and a new Git repoFor the first screen, you’ll need a dropdown that lists a possible “From” airport and a possible “To” airport. just assume all flights fly between SFO and NYC. You’ll need to set up a dropdown menu that contains all eligible flights.
This requires relatively vanilla forms that happen to be prepopulated with collections of data. Working with dates will cover a bit of new ground for you.
db/seeds.rb
file to create several of them.id
s, start datetime, and flight duration).Flight.first.from_airport
, Flight.first.to_airport
and get back Airport objects. Same for Airport.first.departing_flights
and Airport.first.arriving_flights
, which should return a list of flight objects./flights
) the root route./flights
index page to submit using a GET (not POST) request back to the same URL.Once search results are returned, the user just needs to choose from among the eligible flights.
/flights?from_code=SFO&to_code=NYC&date=20131215&num_tickets=2
) and should pull the flights which match that criteria from your database through smart querying model methods.app/views/flights/index.html.erb
view. Make it so that the view will display the results below (if present).#new
action of the BookingsController you’re about to create using another GET method. You’ll need to include a hidden field that contains the number of passengers.Once the user has submitted their chosen flight, it’s time to take their booking information. You’ll need a form to submit the Booking and also create a Passenger object for each passenger who needs a ticket. Don’t worry about creating a separate “Ticket” object, we’ll assume the airline will issue those once the booking is verified.
#new
action, which should have received the flight ID and passenger number parameters, and use it to help render a form for a new booking which displays the currently chosen date, airports, flight ID and a set of fields to enter personal information for each passenger. You’ll want to create a new blank Passenger object in your controller for each passenger, and then use #fields_for
in the view to set up the sub-forms.#create
action to create a new Booking (linking it to the appropriate Flight) but it will also need to accept the nested attributes for each of the Passenger objects and create a new Passenger from them. Be mindful of whitelisting the nested parameters as well.#accepts_nested_attributes_for
method. See the Rails API for examples and this SO post for another.#show
page which displays the booking information (flight and passenger information).Send us your solution so we can show others! Submit a link to the Github repo with your files in it here using any of the methods listed on the contributing page. Please include your partner’s github handle somewhere in the description if they would like attribution.
This section contains helpful links to other content. It isn’t required, so consider it supplemental for if you need to dive deeper into something.
5-6 months
Job Guarantee
1-on-1 Mentorship