Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 无法在创建后使其工作_Ruby On Rails - Fatal编程技术网

Ruby on rails 无法在创建后使其工作

Ruby on rails 无法在创建后使其工作,ruby-on-rails,Ruby On Rails,我正在使用RubyonRails 4.1为我的示例项目创建一个机票预订应用程序。三是三种模式——活动、门票和预订。活动有许多门票和预订。门票有很多预订,属于活动。预订属于活动和门票 以下是机票型号: class Ticket < ActiveRecord::Base belongs_to :event has_many :bookings belongs_to :user validates :ticket_name, :terms_conditions, presenc

我正在使用RubyonRails 4.1为我的示例项目创建一个机票预订应用程序。三是三种模式——活动、门票和预订。活动有许多门票和预订。门票有很多预订,属于活动。预订属于活动和门票

以下是机票型号:

class Ticket < ActiveRecord::Base
  belongs_to :event
  has_many :bookings
  belongs_to :user

  validates :ticket_name, :terms_conditions, presence: true
  validates_date :booking_start_date, on: :create, on_or_after: :today
  validates_date :booking_end_date, after: :booking_start_date
  validates :ticket_price, presence: true, numericality: true
  validates :ticket_quantity, :minimum_quantity, :maximum_quantity, presence: true, numericality: { only_integer: true }

  before_create :check_start_date
  before_update :check_start_date


    def check_start_date
        if (self.booking_start_date >= DateTime.now) && (self.booking_end_date != DateTime.now)
            self.status = 'Open'
        else
            self.status = 'Closed'
        end
    end

    def maximum_tickets_allowed
        (1..maximum_quantity.to_i).to_a
    end
end
classticket=DateTime.now)&(self.booking\u end\u date!=DateTime.now)
self.status='Open'
其他的
self.status='已关闭'
结束
结束
def允许的最大票数
(1)最大数量至
结束
结束
预订模式:

    class Booking < ActiveRecord::Base

      belongs_to :event
      belongs_to :ticket
      has_many :charges

      validates :buyer_name, presence: true
      validates :order_quantity, presence: true, numericality: { only_integer: true }
      validates :email, presence: true, format: { with: /\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z/ }

      def total_amount
        ticket.ticket_price.to_i * order_quantity.to_i
      end

      def check_ticket_count
        count = ticket.ticket_quantity.to_i - order_quantity.to_i
        ticket.update_attribute(:ticket_quantity, count)
      end
    end

The bookings controller:

class BookingsController < ApplicationController
  before_action :authenticate_user!, only: [:index, :destroy]

  def index
    @event = Event.find(params[:event_id])
    #@bookings = @event.bookings.all
    @bookings = @event.bookings.paginate(page: params[:page], per_page: 10)
  end

  def new
    @event = Event.find(params[:event_id])
    @ticket = @event.tickets.find(params[:ticket_id])
    @booking = Booking.new
  end

  def create
    @event = Event.find(params[:event_id])
    @ticket = @event.tickets.find(params[:ticket_id])
    @booking = @event.bookings.create(booking_params)
    @booking.ticket = @ticket

    Stripe.api_key = Rails.configuration.stripe[:secret_key]
    #token = params[:stripeToken]
    @amount = @booking.total_amount

    begin
      customer = Stripe::Customer.create(
        :email => @booking.email,
        :card  => params[:stripeToken]
        )

      charge = Stripe::Charge.create(
        :customer    => customer.id,
        :amount => @amount,
        :currency => "usd",
        #:card => token
        )
      flash[:notice] = "Thanks for the order"
    rescue Stripe::CardError => e
      flash[:danger] = e.message
    end  
    if @booking.save
        BookingMailer.booking_confirmation_user(@booking).deliver
        flash[:notice] = "You've successfully booked the tickets!"
        redirect_to [@event, @booking]
    else
        render 'new'
    end
  end

  def show
    @event = Event.find(params[:event_id])
    @booking = @event.bookings.find(params[:id])
  end

  def destroy
    @event = Event.find(params[:event_id])
    @booking = @event.bookings.find(params[:id])
    @booking.destroy
    redirect_to event_bookings_path
  end

  private
  def booking_params
    params.require(:booking).permit(:buyer_name, :email, :mobile, :address, :order_quantity, :ticket_id)
  end
end
班级预订@booking.email,
:card=>params[:stripeToken]
)
charge=Stripe::charge.create(
:customer=>customer.id,
:amount=>@amount,
:货币=>“美元”,
#:卡=>令牌
)
flash[:notice]=“感谢您的订单”
救援条带::CardError=>e
闪光[:危险]=e.信息
结束
如果@booking.save
BookingMailer.booking\u confirmation\u用户(@booking).deliver
flash[:notice]=“您已成功预订车票!”
将_重定向到[@event,@booking]
其他的
呈现“新”
结束
结束
def秀
@event=event.find(参数[:event\u id])
@booking=@event.bookings.find(参数[:id])
结束
def销毁
@event=event.find(参数[:event\u id])
@booking=@event.bookings.find(参数[:id])
@毁灭
重定向到事件预订路径
结束
私有的
def预订参数
参数要求(:预订)。许可证(:买家姓名,:电子邮件,:手机,:地址,:订单数量,:票证号)
结束
结束

只要我不在创建后添加:check\u ticket\u count方法,Booking.rb中的check\u ticket\u count方法就可以正常工作。当我在创建方法之后添加时,应用程序抛出“未定义的方法`ticket\u quantity'for nil:NilClass”错误。如何通过此操作?

看起来您应该首先将机票与预订关联起来,然后再创建预订

@booking = @event.bookings.new(booking_params)
@booking.ticket = @ticket
@booking.save

希望下次你能用更少的代码提问。

你是说我应该用bookings.create替换bookings.new吗?至少你应该试试。通常没有人会帮你解决这些问题;)