Ruby on rails Rails-如何为活动分配预订号码
我正在使用Ruby on Rails构建一个事件应用程序。活动可以是免费的,也可以是付费的。在每种情况下,我都希望能够为用户在预订活动地点时分配一个唯一的预订号码。 我需要做什么来确保付费和免费活动都在服务器中分配了一个号码,然后确认预订?它是否像在我的预订表中添加预订编号列那样简单 这是我的模型和控制器代码- booking.rbRuby on rails Rails-如何为活动分配预订号码,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我正在使用Ruby on Rails构建一个事件应用程序。活动可以是免费的,也可以是付费的。在每种情况下,我都希望能够为用户在预订活动地点时分配一个唯一的预订号码。 我需要做什么来确保付费和免费活动都在服务器中分配了一个号码,然后确认预订?它是否像在我的预订表中添加预订编号列那样简单 这是我的模型和控制器代码- booking.rb class Booking < ActiveRecord::Base belongs_to :event belongs_to
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
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 @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>
班级预订e
错误。添加(:base,e.message)
假的
结束
结束
#结束
结束
结束
预订_controller.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
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 @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>
class BookingsController
这是我的视图代码。这里有一个if/else语句,这取决于它是否是付费活动。免费活动“预订id”目前显示为空白-不确定原因
booking.new.html.erb
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
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 @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>
您的预订确认书
你好
您已预订了
您的订单号是
我们希望你有一个美好的时光。享受吧
妈妈的爱最能说明问题
确认您的预订
确认您希望在此处预订的空间数:
总金额
£0
卡号
CVC
有效期(年月日)
/
您可以在预订表中创建一个新列,如booking\u id
,并在创建预订对象之前分配一个唯一的随机值
参考:
如果您想让随机数据为人类可读,您可以尝试,这样您就可以像这样使用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) ...
你在哪里保存
booking\u id
@Mike你可以在保存预订对象之前分配一个随机数,对吗?你的意思是“它可以是随机的吗?”。是的,不必特别说明。您的免费和付费模式是分开的还是相同的模式?在“活动”模式下都有。我应该拆分它们吗?如何确保它同时分配给付费和免费活动?我需要在服务器上记录这两种类型的预订。通过这种方式,我可以创建一个预订系统,用于