Rails – 如何显示预订确认详情

我目前正在使用rails构建一个Events应用程序,我在预订确认方面有点挣扎,以及如何向预订参加活动的用户展示。

这是我在bookings_controller中的代码 –

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 end def create # actually process the booking @event = Event.find(params[:event_id]) @booking = @event.bookings.new(booking_params) @booking.user = current_user if @booking.paid_booking flash[:success] = "Your place on our event has been booked" @booking.update_attributes!(booking_number: "MAMA" + '- ' + SecureRandom.hex(4).upcase) redirect_to event_booking_path(@event) else flash[:error] = "Booking unsuccessful" render "new" end end def free_booking if @booking.free_booking @booking.update_attributes!(booking_number: "MAMA" + '- ' + SecureRandom.hex(4).upcase) redirect_to event_booking_path(@event) else flash[:error] = "Booking unsuccessful" render "new" end end def show @event = Event.find(params[:event_id]) @booking = @event.bookings.new @booking = Booking.find_by(params[:booking_number]) end def update puts params @event = Event.find(params[:event_id]) @booking = @event.bookings.new(booking_params) if @booking.save redirect_to event_booking_path , 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 end 

这是免费和付费预订的表格视图 –

new.html.erb

  

Confirm Your Booking

prevented this Booking from saving:

Please confirm the number of spaces you wish to reserve for this event.

This is a free event. No payment is required.

Confirm Your Booking

Confirm number of spaces you wish to book here:

Total Amount £<span class="total" data-unit-cost="">0

/

而我的模特 –

Booking.rb

 class Booking  e errors.add(:base, e.message) false end #end #end end end 

在我的节目视图中,我有以下免费预订代码。 在这种情况下,我只想确认(在此阶段)活动标题,他们要求的空间数量和唯一的预订号码 –

  

Hi there

You have placed a booking on .

You have reserved spaces for this event.

Your booking number is

We hope you have a wonderful time. Enjoy!

目前,当我进行测试预订时,我在我的观点中得到了这个 – 预订确认通知

因此,基本上,数量和数量的具体细节没有显示。 在我的预订表中,我有一个event_id和user_id。 即使每个预订都吸引了一个id,我实际上并没有将booking_id作为我预订表中的一栏 – 这有什么不同吗?

schema.rb

 create_table "bookings", force: :cascade do |t| t.integer "event_id" t.integer "user_id" t.string "stripe_token" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "quantity", default: 1 t.integer "total_amount" t.string "stripe_charge_id" t.string "booking_number" end 

我确信这是显而易见的,但我没有发现它。 我错过了什么?

我不确定@booking是否持久存储到数据库中。 new创建对象但不保存它。 尝试使用调用new然后savecreate方法

  def update @event = Event.find(params[:event_id]) if @event.bookings.create(booking_params) redirect_to event_booking_path , notice: "Booking was successfully updated!" else render 'new' end end 

编辑我实际上会使用new ,然后像这样save

  def update @event = Event.find(params[:event_id]) @booking = @event.bookings.new(booking_params) if @booking.save redirect_to event_booking_path , notice: "Booking was successfully updated!" else render 'new' end end 

如果save对象, save方法将返回true或false,其结果可以在条件中使用。

另一方面, Create将返回模型,无论对象是否已保存。 这意味着if语句将始终为true。

因此,使用new + save将允许您实际查看预订是否已保存或是否发生了其他错误。