2016-08-10 7 views
0

私はRailsを使ってイベントアプリを構築しています。現在のところ、予約手続きでは1人のユーザーが1予約につき1つの予約のみを許可しています。私はユーザーが複数の/グループ予約を一度に行うことができる施設を追加する必要があります。それで、私は「量」機能を必要とするだけでなく、これを価格と比較するためにも必要です。私はSOとGoogleが答えを見つけようとしているが、ほとんどのサイトではかなり一般的なものはどこにもない。Rails - 同じユーザーに複数の予約を追加する方法

は、私はいくつかの種類の方法を必要とすると仮定(モデルに?) -

def total_amount 
    @booking.quantity * @event.price 
end 

だから、誰かが、その後の合計額をスペースあたり£10の費用がかかるイベントに10個のスペースのために予約したい場合100ポンドになるはずです。

私のモデル/コントローラでメソッドを使用していますか?グループ予約ごとの価格を反映させるためにjavascriptが必要ですか?

Booking.rb

 class Booking < ActiveRecord::Base 

    belongs_to :event 
    belongs_to :user 





end 

bookings_controller.rb

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 
     # which person is booking the event? 
     @booking.user = current_user 
     #@booking.quantity = @booking.quantity 
     #@total_amount = @booking_quantity.to_f * @event_price.to_f 

    end 




def create 
     # actually process the booking 
     @event = Event.find(params[:event_id]) 
     @booking = @event.bookings.new(booking_params) 
     @booking.user = current_user 
     #@total_amount = @booking.quantity.to_f * @event.price.to_f 

     Booking.transaction do 

      @event.reload 
      if @event.bookings.count > @event.number_of_spaces 
      flash[:warning] = "Sorry, this event is fully booked." 
      raise ActiveRecord::Rollback, "event is fully booked" 
      end 
     end 




     if @booking.save 

      # CHARGE THE USER WHO'S BOOKED 
      # #{} == puts a variable into a string 
      Stripe::Charge.create(amount: @event.price_pennies, currency: "gbp", 
       card: @booking.stripe_token, description: "Booking number #{@booking.id}") 

      flash[:success] = "Your place on our event has been booked" 
      redirect_to event_path(@event) 
     else 
      flash[:error] = "Payment unsuccessful" 
      render "new" 
     end 

     if @event.is_free? 

      @booking.save! 
      flash[:success] = "Your place on our event has been booked" 
      redirect_to event_path(@event) 
     end 
    end 



    private 

    def booking_params 
     params.require(:booking).permit(:stripe_token, :quantity) 
    end 



end 

new.html.erb(予約) -

ここで予約するためのMVCコードです

   <% if @event.is_free? %> 
<div class="col-md-6 col-md-offset-3" id="eventshow"> 
    <div class="row"> 
    <div class="panel panel-default"> 
     <div class="panel-heading"> 
      <h2>Your Booking Confirmation</h2> 
     </div> 
       <div class="panel-body"> 

         <h1>Hi there</h1> 

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

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

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

         <p>Love from Mama Knows Best</p> 
       </div>   
        <div class="panel-footer"> 
        <%= link_to "Home", root_path %> 
        </div> 
    </div> 
    </div> 
</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> 

      <div class="panel-body">  

       <p>Total Amount<%= @event.price %></p> 
       <%= simple_form_for [@event, @booking], id: "new_booking" do |form| %> 





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

<script type="text/javascript" src="https://js.stripe.com/v2/"></script> 

<script type="text/javascript"> 
    Stripe.setPublishableKey('<%= STRIPE_PUBLIC_KEY %>'); 
    var stripeResponseHandler = function(status, response) { 
    var $form = $('#new_booking'); 

    if (response.error) { 
    // Show the errors on the form 
    $form.find('.payment-errors').text(response.error.message); 
    $form.find('input[type=submit]').prop('disabled', false); 
    } else { 
    // token contains id, last4, and card type 
    var token = response.id; 
    // Insert the token into the form so it gets submitted to the server 
    $form.append($('<input type="hidden" name="booking[stripe_token]"  />').val(token)); 
    // and submit 
    $form.get(0).submit(); 
    } 
    }; 

    // jQuery(function($) { - changed to the line below 
    $(document).on("ready page:load", function() { 

    $('#new_booking').submit(function(event) { 
     var $form = $(this); 

     // Disable the submit button to prevent repeated clicks 
     $form.find('input[type=submit]').prop('disabled', true); 

     Stripe.card.createToken($form, stripeResponseHandler); 

     // Prevent the form from submitting with the default action 
     return false; 
    }); 
    }); 
</script> 

ここでは、予約やイベントのためのスキーマである -

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

create_table "events", force: :cascade do |t| 
t.string "title" 
t.string "location" 
t.date  "date" 
t.time  "time" 
t.text  "description" 
t.string "organised_by" 
t.datetime "created_at",       null: false 
t.datetime "updated_at",       null: false 
t.integer "user_id" 
t.string "image_file_name" 
t.string "image_content_type" 
t.integer "image_file_size" 
t.datetime "image_updated_at" 
t.integer "category_id" 
t.string "url" 
t.integer "number_of_spaces" 
t.integer "price" 
t.boolean "is_free" 
t.integer "price_cents",  default: 0,  null: false 
t.integer "price_pennies",  default: 0,  null: false 
t.string "price_currency",  default: "GBP", null: false 
t.boolean "happened",   default: false 
    end 

私は、ユーザーが指定するためにそれらを要求する「ブックイベント」をクリックすると、理想的に、私はアクティブに補足部分フォーム必要がある、と思いますお支払いの手続きを行う前に、イベント予約のためのスペースを確保してください。適切な方向への支援やプッシュは感謝します。

答えて

0

モデル内に仮想属性を作成する(推奨)モデル全体の価格で予約するのが良い方法です。

booking.rb

def total_amount 
    self.quantity * self.event.price 
end 

彼は(少なくとも、より良いUX用)確認ページを見ている前に、ユーザーがスペースの数を指定してみましょう。 「新しい」方法1.転送スペースの数に設定し、デフォルト:

def new 
    .... 
    @booking = @event.bookings.new(quantity: params[:quantity]) 
    .... 

end 

は今、あなたは、私が補足部分を構築するか、含まない、だから、

<p>Total Amount<%= @booking.total_amount %></p> 
+0

で合計金額を表示したり、アクセスすることができますこれはすべて支払いフォームにありますか?それは働くだろうか? –

+0

あなたは部分的な必要はありません。 total_priceを通常の属性と考えてください。それは動作する必要があります – user1201917

+0

上記のnew.htmlフォームで、どこでユーザーがスペースの数を指定できるのでしょうか?入力フォームを含めるだけですか? –

関連する問題