I'm building an events site using Rails and I'm not sure whether my database is capturing bookings as they happen. I'm still in development mode so any bookings are purely 'mock' for demo purposes. How do I check whether they're being logged ( I'm pretty sure they're not) and if they're not - how do I remedy this?
Here's the relevant code as things stand -
booking.rb
class Booking < ActiveRecord::Base
belongs_to :event
belongs_to :user
end
bookings_controller.rb
class BookingsController < ApplicationController
before_action :authenticate_user!
def new
@event = Event.find(params[:event_id])
@booking = @event.bookings.new
# which person is booking the event?
@booking.user = current_user
@booking.quantity = @booking.quantity
@total_amount = @booking_quantity.to_f * @event_price.to_f
end
def create
# actually process the booking
@event = Event.find(params[:event_id])
@booking = @event.bookings.new(booking_params)
@booking.user = current_user
if @booking.save
Stripe::Charge.create(amount: @event.price_pennies, currency: "gbp",
card: @booking.stripe_token, description: "Booking number #{@booking.id}")
flash[:success] = "Your place on our event has been booked"
redirect_to event_path(@event)
else
flash[:error] = "Payment unsuccessful"
render "new"
end
if @event.is_free?
flash[:success] = "Your place on our event has been booked"
redirect_to event_path(@event)
end
end
private
def booking_params
params.require(:booking).permit(:stripe_token, :quantity)
end
end
schema.rb
create_table "bookings", force: :cascade do |t|
t.integer "event_id"
t.integer "user_id"
t.string "stripe_token"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "quantity"
Do I need to add a booking_id to the bookings table?