- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 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/
我正在为一家小型酒店开发预订系统。我非常擅长 PHP,但不太擅长 SQL...我制作了一个表格,您可以在其中输入您的信息、房间数量并使用日历选择到达日期和退房日期。 现在一切都很顺利,直到我不得不检查
我有一个租车脚本。我把汽车放在汽车 table 上。我将所有预订信息保存在预订表中。预订表有开始日期和结束日期字段以及 car_id 字段。 例如:car_id: 14, from="2014-06-
我最近被问及一个关于假设的基于 Web 的预订系统的面试问题,以及我将如何设计数据库架构以最大限度地减少重复并最大限度地提高灵 active 。 用例是管理员将属性的可用性输入系统。可以设置多个时间段
如何在 中使用标签式标题预订 像 RMarkdown 一样? 在 RMarkdown 中: # heading1 {.tabset} ## tab1 content1 ## tab2 content2
我使用 Woothemes booking对于我的 woocommerce 网站 当我为产品建立自定义搜索时,我还必须搜索可用性,这是我应该如何管理来自 B.O 的可用性以及插件如何存储到数据库 a:
我正在尝试找到一种在亚马逊中按 ISBN 查找书名的简单方法。我正在协助我 child 的学校做一个简单的 list 。我的测试集中的很多书籍都无法使用其他来源(尝试使用 Google、WorldCa
我正在使用 JQuery-Mobile 在浏览器中开发一个房间预订应用程序。最初,我将预订建模为带有时间戳的记录和带有持续时间的字段。 现在,如果我有一个表/div,每个表代表一天的 15 分钟,那么
我有一个自托管 NancyFX Web 服务器的桌面应用程序。作为一个桌面应用程序,我们需要允许动态 IP 地址,因此我们使用 netsh 的通配符选项注册了 url,如下所示: netsh http
我有一个关于 x++ 和循环内容的问题要问。首先,这是代码: class MixFor5 { public static void main(String[] args) { int x = 0;
我在为带有等候名单的预订类型系统设计算法时遇到困难。 在任何给定时间都可以有 3 个活跃插槽,每个插槽持续一个月,之后它们将被放入等待列表中。我希望能够插入项目从等待状态转为事件状态的日期。 我知道三
我正在制作一个 session 室预订系统,您可以在其中选择特定的时间段,然后您应该在那时获得可用的房间。一些表的名称是法语,因此我将在需要时提供 () 之间的含义。我的问题是,当我选择时间时,我什至
我开始了一个新项目: 我只运行以下命令: rails g model subscription 然后: rails g controller subscriptions 然后: 已将resources
我正在通过一个 api 发布一个现场预订请求。当我通过一个表单操作发布数据并提交时,我会得到回复。但是当我试图通过 curl 发布相同的 url 以获得响应时,我没有得到任何数据。 网址是:- htt
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的非现场资源的问题对于 Stack Overflow 来说是无关紧要的
我想知道有用的资源/书籍,它们将有助于初学者学习网络服务器、应用程序服务器和网络架构基础知识。 我正在尝试了解不同的服务器如何相互交互。浏览器如何向服务器发送请求以及服务器如何发送响应。我有兴趣了解这
我正在开发一个项目,用户可以在餐厅进行预订,但仅限于餐厅允许的日期。餐厅还可以设置特定时间有多少个座位。 因此,餐厅可以提供每周格式的时间表,但也可以提供异常(exception)情况(例如假期)。
我正在尝试为以下计划改进 SQL: 看起来像这样: - @users.each do |user| - @dates.each do |date| %td - Booking.
我正在使用 WooCommerce 预订插件,我目前希望在预订摘要(产品选项)中显示其他信息。 为此,我使用了以下钩子(Hook):woocommerce_admin_booking_data_aft
请让我了解保留所有 ec2 实例时 ssd 的费用,那么为什么每 GB 月会扣除 0.10 美元? 我已经保留了 c4.2xlarge 和 m4.xlarge 实例,但仍然会从账单中连续扣除费用,仅针
我想知道是否有办法使用 Woocommerce 预订自动设置可预订产品的开始日期和结束日期。我想要实现的是,当用户进入我的产品页面时,开始日期和结束日期(月、日和年)将根据我希望通过 Jquery 设
我是一名优秀的程序员,十分优秀!