-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* decrement available_seats on flight after_create booking TODO: * don't show: * flights w/o available_seats * flights that have departed * instead of redirect_to root, should go to a booking/new page * this should be its own separate form that lists visible attrs of the flight (num passengers, depart date, arr/depts, etc) * should be a form with fillable fields for num seats requested. * the rest is coming from the logged in user (their email, eg) and the booking.flight association fix: @booking, strong params TODO: validate selected flight has more than 0 available seats * successfully submit/create booking w/o validations
- Loading branch information
1 parent
18d6d20
commit b86cfcc
Showing
8 changed files
with
45 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,4 @@ | |
tr:nth-child(odd) { | ||
background-color: #f9f9f9; | ||
} | ||
|
||
/* better font */ | ||
font-family: 'Open Sans', | ||
sans-serif; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,30 @@ | ||
class BookingsController < ApplicationController | ||
def new | ||
# designate the @flight instance to the one that was selected on the | ||
# previous page, flights#index | ||
@booking = Booking.new | ||
end | ||
|
||
def create | ||
@booking = Booking.new(params[:user_]) | ||
@passenger = current_user | ||
@booking.save ? flash[:notice] = "Booking created!" : flash[:alert] = "Booking failed!" | ||
redirect_to root_path | ||
@booking = Booking.new(booking_params) | ||
@booking.passenger = current_user | ||
if @booking.save | ||
flash[:notice] = "Booking created!" | ||
redirect_to root_path | ||
else | ||
flash[:alert] = "Booking failed!" | ||
redirect_to root_path, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def booking_params | ||
params.require(:booking).permit(:flight_id, :passenger_id) | ||
end | ||
|
||
def logged_in_user | ||
unless current_user | ||
flash[:danger] = "Please log in." | ||
redirect_to root_path, status: :unauthorized | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,20 @@ | ||
class Booking < ApplicationRecord | ||
belongs_to :passenger, class_name: "User", foreign_key: "passenger_id" | ||
belongs_to :flight | ||
|
||
validates :passenger_id, :flight_id, presence: true | ||
# ensure flight has avaialble seats > 0 | ||
validate :decrement_seats | ||
|
||
after_create :decrement_seats | ||
|
||
accepts_nested_attributes_for :passenger | ||
|
||
private | ||
|
||
# TODO: determine passengers arg | ||
def decrement_seats(passengers = 1) | ||
flight.available_seats -= passengers | ||
flight.save | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters