我正在使用Ruby on Rails 4.1为我的示例项目创建一个票务预订应用程序。三个是三个模型 - 活动,门票和预订。活动有很多门票和预订。门票有很多预订,属于活动。预订属于活动和门票。
这是票证模型:
class Ticket < ActiveRecord::Base
belongs_to :event
has_many :bookings
belongs_to :user
validates :ticket_name, :terms_conditions, presence: true
validates_date :booking_start_date, on: :create, on_or_after: :today
validates_date :booking_end_date, after: :booking_start_date
validates :ticket_price, presence: true, numericality: true
validates :ticket_quantity, :minimum_quantity, :maximum_quantity, presence: true, numericality: { only_integer: true }
before_create :check_start_date
before_update :check_start_date
def check_start_date
if (self.booking_start_date >= DateTime.now) && (self.booking_end_date != DateTime.now)
self.status = 'Open'
else
self.status = 'Closed'
end
end
def maximum_tickets_allowed
(1..maximum_quantity.to_i).to_a
end
end
预订模型:
class Booking < ActiveRecord::Base
belongs_to :event
belongs_to :ticket
has_many :charges
validates :buyer_name, presence: true
validates :order_quantity, presence: true, numericality: { only_integer: true }
validates :email, presence: true, format: { with: /\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z/ }
def total_amount
ticket.ticket_price.to_i * order_quantity.to_i
end
def check_ticket_count
count = ticket.ticket_quantity.to_i - order_quantity.to_i
ticket.update_attribute(:ticket_quantity, count)
end
end
The bookings controller:
class BookingsController < ApplicationController
before_action :authenticate_user!, only: [:index, :destroy]
def index
@event = Event.find(params[:event_id])
#@bookings = @event.bookings.all
@bookings = @event.bookings.paginate(page: params[:page], per_page: 10)
end
def new
@event = Event.find(params[:event_id])
@ticket = @event.tickets.find(params[:ticket_id])
@booking = Booking.new
end
def create
@event = Event.find(params[:event_id])
@ticket = @event.tickets.find(params[:ticket_id])
@booking = @event.bookings.create(booking_params)
@booking.ticket = @ticket
Stripe.api_key = Rails.configuration.stripe[:secret_key]
#token = params[:stripeToken]
@amount = @booking.total_amount
begin
customer = Stripe::Customer.create(
:email => @booking.email,
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:currency => "usd",
#:card => token
)
flash[:notice] = "Thanks for the order"
rescue Stripe::CardError => e
flash[:danger] = e.message
end
if @booking.save
BookingMailer.booking_confirmation_user(@booking).deliver
flash[:notice] = "You've successfully booked the tickets!"
redirect_to [@event, @booking]
else
render 'new'
end
end
def show
@event = Event.find(params[:event_id])
@booking = @event.bookings.find(params[:id])
end
def destroy
@event = Event.find(params[:event_id])
@booking = @event.bookings.find(params[:id])
@booking.destroy
redirect_to event_bookings_path
end
private
def booking_params
params.require(:booking).permit(:buyer_name, :email, :mobile, :address, :order_quantity, :ticket_id)
end
end
只要我没有添加after_create:check_ticket_count方法,Booking.rb中的check_ticket_count方法就可以正常工作。在我添加after_create方法的那一刻,app会抛出&#34;未定义的方法`ticket_quantity&#39;为零:NilClass&#34;错误。如何摆脱这个?
答案 0 :(得分:0)
看起来您应首先将机票与预订相关联,然后才能创建预订。
@booking = @event.bookings.new(booking_params)
@booking.ticket = @ticket
@booking.save
希望下次你能用更少的代码提问。