gpt4 book ai didi

ruby-on-rails - Rails - 如何为事件分配预订编号

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:12 25 4
gpt4 key购买 nike

我正在使用 Ruby on Rails 构建一个事件应用程序。事件可以免费或付费。在每种情况下,我都希望能够在用户预订事件席位时为他们分配一个唯一的预订编号。我需要做什么才能确保在服务器中为付费和免费事件分配一个编号,然后确认预订?是不是就像在我的预订表中添加一个预订编号列一样简单?

这是我的模型和 Controller 代码 -

booking.rb

      class Booking < ActiveRecord::Base

belongs_to :event
belongs_to :user

#validates :quantity, presence: true, numericality: { greater_than: 0 }
validates :total_amount, presence: true, numericality: { greater_than: 0 }
validates :quantity, :total_amount, presence: true


def reserve
# Don't process this booking if it isn't valid
self.valid?

# We can always set this, even for free events because their price will be 0.
#self.total_amount = booking.quantity * event.price

# Free events don't need to do anything special
if event.is_free?
save!

# Paid events should charge the customer's card
else

begin
self.total_amount = event.price_pennies * self.quantity
charge = Stripe::Charge.create(
amount: total_amount,
currency: "gbp",
source: stripe_token,
description: "Booking created for amount #{total_amount}")
self.stripe_charge_id = charge.id
save!
rescue Stripe::CardError => e
errors.add(:base, e.message)
false
end
end
#end
end
end

bookings_controller.rb

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
#@total_amount = @event.price * @booking.quantity


end

def create
puts params
# actually process the booking
@event = Event.find(params[:event_id])
@booking = @event.bookings.new(booking_params)
@booking.user = current_user

if
@booking.reserve
flash[:success] = "Your place on our event has been booked"
redirect_to event_path(@event)
else
flash[:error] = "Booking unsuccessful"
render "new"
end
end


private

def booking_params
params.require(:booking).permit(:stripe_token, :quantity, :event_id, :stripe_charge_id, :total_amount)
end



end

这是我的观点代码。这里有一个 if/else 语句,取决于它是否是付费事件。免费事件“booking_id”目前只是显示为空白 - 不知道为什么。

booking.new.html.erb

                <% if @event.is_free? %>
<div class="col-md-6 col-md-offset-3" id="eventshow">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h2>Your Booking Confirmation</h2>
</div>
<div class="panel-body">

<h1>Hi there</h1>

<p>You have placed a booking on <%= @event.title %></p>

<p>Your order number is <%= @booking.booking_id %></p>

<p>We hope you have a wonderful time. Enjoy!</p>

<p>Love from Mama Knows Best</p>
</div>
<div class="panel-footer">
<%= link_to "Home", root_path %>
</div>
</div>
</div>
</div>

<% else %>

<div class="col-md-6 col-md-offset-3" id="eventshow">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h2>Confirm Your Booking</h2>
</div>



<%= simple_form_for [@event, @booking], id: "new_booking" do |form| %>

<div class="calculate-total">
<p>
Confirm number of spaces you wish to book here:
<input type="number" placeholder="1" name="booking[quantity]" min="1" value="1" class="num-spaces">
</p>
<p>
Total Amount
£<span class="total" data-unit-cost="<%= @event.price %>">0</span>
</p>
</div>



<span class="payment-errors"></span>

<div class="form-row">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number"/>
</label>
</div>

<div class="form-row">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc"/>
</label>
</div>

<div class="form-row">
<label>
<span>Expiration (MM/YYYY)</span>
<input type="text" size="2" data-stripe="exp-month"/>
</label>
<span> / </span>
<input type="text" size="4" data-stripe="exp-year"/>
</div>
</div>
<div class="panel-footer">

<%= form.button :submit %>


</div>

<% end %>
<% end %>

</div>
</div>
</div>

最佳答案

因此您可以像这样使用 SecureRandom 生成器,并假设您的模型名为 Booking

  ### Add this in your model
before_create :add_booking_number
validates_uniqueness_of :add_booking_number

def add_booking_number
begin
self.booking_number = SecureRandom.hex(2).upcase
other_booking = booking_number.find_by(booking_number: self.booking_number)
end while other_booking
end

To use the Moniker "MAMA" you can do

def add_event_ident
self.booking_number = "MAMA" + '- ' + SecureRandom.hex(2).upcase
...
end

.hex 末尾的(2) 控制生成字符串的长度(2) 会产生4 个随机字母。您可以通过进入 Rails 控制台并使用它来找到最适合您的长度。

SecureRandom.hex(4) ...

关于ruby-on-rails - Rails - 如何为事件分配预订编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40178525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com