建立活动预订流程

时间:2016-06-06 19:02:36

标签: ruby-on-rails model

堆叠新手!那你好!我正在制作一个样本事件预订应用程序,它使用条带检查事件。

我的设置在

之下
class Event < ActiveRecord::Base
   belongs_to :user
   has_many :tickets, :inverse_of => :event, dependent: :destroy
end


class Ticket < ActiveRecord::Base 
  belongs_to :event, :inverse_of => :tickets
end 


class Booking < ActiveRecord::Base
   belongs_to :event
   belongs_to :ticket, :inverse_of => :bookings
   has_one :sale, :inverse_of => :booking
end

class Sale < ActiveRecord::Base
    belongs_to :booking, :inverse_of => :sale
    belongs_to :ticket
end  


class BookingsController < ApplicationController
  before_filter :load_event
  before_filter :load_ticket

  def index
    @bookings = @event.bookings
  end

  def new
    @booking = Booking.new
  end

  private

  def load_event
    @event =  Event.find(params[:event_id])
  end
  def load_ticket
    @ticket = @event.tickets.find(params[:ticket_id])
  end

  def booking_params
    params.require(:booking).permit(:buyer_name, :phone, :address, :order_quantity,:total_amount)
  end
end



class TransactionsController < ApplicationController
  before_filter :load_event
  before_filter :load_booking
  before_filter :load_ticket

 def new
  end

 def pickup
 @sale = Sale.find_by!(guid: params[:guid])
 @booking = @sale.booking
 end

 def complete 
 @sale = Sale.find_by!(guid: params[:guid])
 @booking = @sale.booking
 end



if sale.save
  StripeCharger.perform_async(sale.guid)
  render json: { guid: sale.guid }
else
  errors = sale.errors.full_messages
  render json: {
    error: errors.join(" ")
  }, status: 400
end
end

def status
 sale = Sale.find_by!(guid: params[:guid])

 render json: { status: sale.state }
end



private

def load_event
@event = Event.find(params[:event_id])
end

def load_booking
   @booking = @event.bookings.find(params[:booking_id])
end

def load_ticket
  @ticket = @booking.ticket.find(params[:ticket_id])
end
end


            #Stripe Checkout Routes 

我遗漏了模型中的最小细节。但基本上我要做的是让用户输入名称和票数量,并从预订重定向提交到新的交易,我可以用条纹检查执行销售模型。

我的一切最终目标是让预订数量输入乘以机票价格,以获得携带Stripe的总金额。有没有人对如何改善这种分解有任何建议。建模事件,门票,预订检查类型的例子。对不起,如果我把它分解为noobish,我试图绕着头来完成这个。

1 个答案:

答案 0 :(得分:1)

在交易控制器中,您不需要在@ booking.ticket上找到

def load_ticket
  @ticket = @booking.ticket.find(params[:ticket_id])
end

由于@booking只有一张票,你只需要@ booking.ticket