【发布时间】:2016-12-23 01:18:05
【问题描述】:
我正在使用 Rails 构建一个事件应用程序。我已经在我的控制器中安装了一个代码块,以确保一旦分配/预订了活动的空间总数,就不会发生超额预订。 问题是它似乎只在付款后才起作用,而不是之前。当用户点击预订活动时,我需要代码来显示一条消息,但目前,它允许用户完成付款过程,拿走他们的钱,然后当它重定向回活动页面时,闪消息弹出。 它也仅适用于付费活动,不适用于免费活动。
这是我的 bookings_controller 代码 -
class BookingsController < ApplicationController
before_action :authenticate_user!
def new
# booking form
# I need to find the event that we're making a booking on
@event = Event.find(params[:event_id])
# and because the event "has_many :bookings"
@booking = @event.bookings.new(quantity: params[:quantity])
# 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
#@total_amount = @booking.quantity.to_f * @event.price.to_f
Booking.transaction do
@event.reload
if @event.bookings.count > @event.number_of_spaces
flash[:warning] = "Sorry, this event is fully booked."
raise ActiveRecord::Rollback, "event is fully booked"
end
end
if @booking.save
# CHARGE THE USER WHO'S BOOKED
# #{} == puts a variable into a string
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?
@booking.save!
flash[:success] = "Your place on our event has been booked"
redirect_to event_path(@event)
end
end
#def total_amount
#@total_amount = @booking.quantity * @event.price
#end
private
def booking_params
params.require(:booking).permit(:stripe_token, :quantity)
end
end
这可以在控制器中调整还是需要模型中的方法?
【问题讨论】:
-
此时您的事务块在 if 块之后结束。这是复制粘贴错误还是您的实际代码?
-
这是实际代码。
-
您是否尝试过将
if语句放在 预订交易块之前?在我看来,只有在活动空间可用而不是之后才调用 Booking 交易。
标签: ruby-on-rails ruby ruby-on-rails-4 model-view-controller paypal