【问题标题】:Rails - How to assign booking numbers to eventsRails - 如何为活动分配预订编号
【发布时间】:2016-10-21 13:54:54
【问题描述】:

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

这是我的模型和控制器代码 -

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>  

【问题讨论】:

  • 你在哪里保存booking_id
  • @Mike 你可以在保存预订对象之前分配一个随机数,对吧?
  • 你的意思是,“它可以是随机的吗?”。是的,它不必具体。
  • 您的免费和付费型号是分开的还是同一个型号?
  • 在“事件”模型下都有。我应该把它们分开吗?

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

所以您可以像这样使用 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) ...

【讨论】:

    【解决方案2】:

    您可以在预订表中创建一个新列,例如booking_id,并在创建预订对象之前分配一个唯一的随机数。

    参考:Source

    如果您希望随机数具有人类可读性,可以尝试this

    【讨论】:

    • 我如何确保它既分配给付费活动又分配给免费活动?我需要将这两种类型的预订记录在服务器中。这样我就可以为两者创建一个预订系统,确保不会发生超额预订。
    • 就像我在第一个参考问题中提到的那样,您可以在“before_create”处调用模型中的方法,该方法将创建一个随机并分配给“booking_id”列,而与免费或付费事件无关。
    猜你喜欢
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多