私はRailsを使ってイベントアプリを構築しています。私は最近、無料のイベントに対応するために私の予約コントローラーの周りを変更しました。このアプリの側面はうまくいくようですが、支払い済みのイベントのテスト予約をしようとすると、支払いの提出フォームを完了した後、アプリはデフォルトで「予約不成功」になります。Railsエラー - 予約に失敗
なぜこれが起こっているのか分かりません。
def new
@event = Event.find(params[:event_id])
@booking = Booking.new
@booking.user = current_user
end
def create
@event = Event.find(params[:event_id])
@booking = Booking.new(booking_params)
@booking.user = current_user
if
@booking.save
flash[:success] = "Your place on our event has been booked"
redirect_to event_booking_path(@event, @booking)
else
flash[:error] = "Booking unsuccessful"
render "new"
end
end
def show
@event = Event.find(params[:event_id])
@booking = Booking.find(params[:id])
end
def update
if @booking.update(booking_params)
redirect_to event_booking_path(@event, @booking) , notice: "Booking was successfully updated!"
else
render 'new'
end
end
private
def booking_params
params.require(:booking).permit(:stripe_token, :booking_number, :quantity, :event_id, :stripe_charge_id, :total_amount)
end
そして、私のモデルでは、私はこれを持っている - -
Booking.rb
class Booking < ActiveRecord::Base
belongs_to :event
belongs_to :user
before_create :set_booking_number
validates :quantity, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :total_amount, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :quantity, :total_amount, :booking_number, presence: true
def set_booking_number
self.booking_number = "MAMA" + '- ' + SecureRandom.hex(4).upcase
end
def booking
# Don't process this booking if it isn't valid
self.valid?
if booking.is_free?
self.total_amount = event.price_pennies.nil?
save!
end
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
self.booking_number = "MAMA" + '- ' + SecureRandom.hex(4).upcase
save!
rescue Stripe::CardError => e
errors.add(:base, e.message)
false
end
end
end
ので、そのは保存しないか、何か他のものではない権利があります何らかの理由でここに私のコントローラロジックです。どんな援助も感謝します
検証エラーを表示することによって、ビューにエラー・メッセージを参照してください - ここに私のコントローラとモデルの最後のコードです。 'フラッシュ[:エラー] = @ booking.errors.full_messages.join( '、')' – usha
あなたのコードは不完全なようです...あなたの予約モデルのあなたの予約メソッドがどこから呼び出されているのかわかりませんが、あなたのコントローラから。また、あなたのモデルでどのようなバリデーションが行われているかを知ることができます。 – David
完全な予約モデルコードがバリデーションとともに追加されました。何かご意見は? booking.is_freeではなく? event.is_freeを使用してif/elseステートメントにする必要がありますか? 'else'メソッドコードを支払った? (下記の他のコメントを参照してください) –