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

时间:2016-11-14 14:14:50

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

我目前正在使用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

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

目前,当我进行测试预订时,我在观看中得到了这个 - Booking confirmation

因此,基本上,没有显示数量和数量的具体细节。在我的预订表中,我有一个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

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

1 个答案:

答案 0 :(得分: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可以让您实际查看预订是否已保存,或者是否发生了其他错误。