Rails-如何显示预订确认信息 [英] Rails - How to show booking confirmation details

查看:63
本文介绍了Rails-如何显示预订确认信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Rails构建事件"应用,并且在预订确认方面以及如何向已预订参加活动的用户展示该内容方面有些挣扎.

I'm currently building an Events app using rails and I'm struggling a little with the booking confirmation side and how to show this to a user who has booked to attend an event.

这是我在bookings_controller中的代码-

This is my code in the 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

这是免费&的表单视图付费预订-

Here's the form view for both free & paid bookings -

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

在我的表演视图中,我有以下代码可免费预订.在这种情况下,我只想确认(在此阶段)事件的标题,他们所请求的空间数量以及唯一的预订号-

In my show views I have the following code for free bookings. In this instance I simply want to confirm (at this stage) the event title, quantity of spaces they've requested and the unique booking number -

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

此刻,当我进行考试预约时,我的想法是-

At the moment, when I perform a test booking I get this in my views -

因此,基本上,没有显示数量和数量的具体细节.在我的预订表中,我有一个event_id和user_id.即使每个预订都吸引了一个ID,但实际上我的预订表中没有我的booking_id作为列-这有什么区别吗?

So, basically, the specific details for quantity and number are not showing. In my bookings table I have an event_id and user_id. Even though each booking attracts an id I dont actually have booking_id as a column in my bookings table - does this make any difference?

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

我敢肯定这很明显,但我没发现.我想念什么?

I'm sure this is something so obvious but I'm failing to spot it. What am I missing?

推荐答案

我不确定@booking是否持久保存到数据库中. new创建对象,但不保存.尝试使用先调用new然后save

I'm not sure @booking is persisted into the database. new creates the object but doesn't save it. Try using the create method which calls new then 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

edit I would actually use new and then save like so

     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,其结果可以在条件中使用.

The save method will return with true or false if the object was saved or not, the result of this can be used in the conditional.

Create都将返回模型.这意味着if语句将始终为true.

Create on the other hand will return the model regardless of whether the object was saved or not. Which means the if statement will always be true.

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

So, using new + save will allow you to actually see if the booking was saved or not or if another error occurred.

这篇关于Rails-如何显示预订确认信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆