【问题标题】:Rails - How to show booking confirmation detailsRails - 如何显示预订确认详情
【发布时间】:2016-11-14 14:14:50
【问题描述】:

我目前正在使用 Rails 构建一个活动应用程序,但在预订确认方面以及如何向已预订参加活动的用户展示这一点时遇到了一些困难。

这是我在 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

<% if @booking.free_booking %>
  <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| %>
                    <% if @booking.errors.any? %>
                        <h2><%= pluralize(@booking.errors.count, "error") %> prevented this Booking from saving:</h2>
                  <ul>
                        <% @booking.errors.full_messages.each do |message| %>
                      <li><%= message %></li>
                    <% end %>
                  </ul>
                <% end %>  

                <div class="form-group">
                    <p>Please confirm the number of spaces you wish to reserve for this event.</p>
                    <%= form.input :quantity, class: "form-control" %>
                </div>  
                 <p> This is a free event. No payment is required.</p>

                   <div class="panel-footer"> 


                     <%= form.submit :submit, label: 'Confirm Booking', class: "btn btn-primary" %>

                     <% end %>
                  </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>  

还有我的模特-

Booking.rb

class Booking < ActiveRecord::Base

    belongs_to :event
    belongs_to :user


  #before_create :set_booking_number 
  #before_validation :set_is_free
     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 set_is_free
    set_is_free = total_amount.nil?
  end


  def free_booking
      self.valid?
        # Free events don't need to do anything special
          if event.is_free?

            save!
          end
  end



    def paid_booking
        # Don't process this booking if it isn't valid
        self.valid?



                # 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
              self.booking_number = "MAMA" + '- ' + SecureRandom.hex(4).upcase
                        save!
                    rescue Stripe::CardError => e
                    errors.add(:base, e.message)
                    false
                end
            #end 
        #end
  end
end

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

                <h1>Hi there</h1>

                <p>You have placed a booking on <%= @event.title %>.</p>

                <p>You have reserved <%= @booking.quantity %> spaces for this event.</p>

                <p>Your booking number is <%= @booking.booking_number %></p>

                <p>We hope you have a wonderful time. Enjoy!</p>

目前,当我执行测试预订时,我的观点是 -

因此,基本上没有显示数量和数量的具体细节。在我的预订表中,我有一个 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.quantity 为零,请检查此\
  • 在我的预订表中,它设置为 - 't.integer "quantity", default: 1'
  • 检查,(可能在 Rails 控制台中)params 散列中的 booking_number 引用的特定预订实际上存在于您的 d/b 中。您也可以删除 show 方法中的第二行。
  • 我通过控制台查看并得到了这个 - Booking.find_by(booking_number: "MAMA- 5D4EFC95") Booking Load (0.3ms) SELECT "bookings".* FROM "bookings" WHERE "预订"."booking_number" = ? LIMIT 1 [["booking_number", "MAMA- 5D4EFC95"]] => #
  • 没有任何预订属性被输出,也许可以尝试从控制器记录@booking 变量以确保它是您所期望的。您是否还为该控制器设置了任何测试,因为这也将帮助您进行调试。

标签: ruby-on-rails ruby ruby-on-rails-4 model-view-controller


【解决方案1】:

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

 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 方法将返回 true 或 false 是否保存对象,其结果可用于条件。

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

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

【讨论】:

  • 这真的应该进入我的“表演”方法吗?而不是“更新”?我想知道我在这里错了是不是我的“表演”方法。
  • 它应该在update 方法中,因为它是表单发布数据以进行处理的地方。
  • 不,这并没有改变任何东西。
  • 你能试试我更新的答案吗?如果这不起作用,请尝试记录 params 对象以查看您的数据是否实际正确发布
  • 我已经尝试了更新的答案,但也没有用。
猜你喜欢
  • 2014-11-24
  • 2015-04-22
  • 2019-12-05
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
相关资源
最近更新 更多