2017-07-14 3 views
3

私はRailsを使い始めましたが、何が起こっているのか分かりません。私が構築しているアプリはオンラインストアです。現在の機能は動作しますが、実装しようとしている変更は機能していません。出発点として、私が現在行っている作業バージョンを示します。ここに私のカートを/ show.html.erbファイルされる:Railsアプリケーションのパラメータに欠損値がある可能性があります

<p id="notice"><%= notice %></p> 

<h2>My Cart</h2> 
<table class="table table-responsive table-striped"> 
    <thead> 
     <tr> 
      <th>Item</th> 
      <th>Quantity</th> 
      <th>Total Price in Galleons</th> 
      <th>Total Price in Muggle Currency</th> 
     </tr> 
     <tbody> 
      <%= render(@cart.line_items) %> 
      <tr> 
       <td>Total</td> 
       <td><%= number_to_currency(@cart.total_price * 7.35) %></td> 
       <td></td> 
       <td></td> 
      </tr> 
     </tbody> 
    </thead> 
</table> 

<br> 

<div class="row"> 
    <div class="col-md-3"> 
     <div class="row"> 
      <div class="col-md-4"> 
       <%= link_to 'Back', products_path, :class => 'btn btn-primary whiteText' %> 
      </div> 
      <div class="col-md-4"> 
       <%= link_to "Checkout", new_charge_path, :class => 'btn btn-success whiteText' %> 
      </div> 
      <div class="col-md-4"> 
       <%= link_to 'Empty Cart', @cart, method: :delete, data: {confirm: 'Are you sure you want to empty your cart?'}, :class => 'btn btn-danger whiteText' %> 
      </div> 
     </div> 
    </div> 
    <div class="col-md-9"></div> 
</div> 

しかし、私はそれがアドレス確認ページにユーザーをリダイレクトするために私の注文足場を使用するように(受注/新しいワークフローを少し変更したいのです.html.erb)をクリックしてください。住所が確認されたら、顧客は支払いページに移動します。これは、現在のCheckoutリンクのnew_charge_pathが既にリダイレクトされていることを示します。/

<%= link_to "Checkout", new_order_path, method: :get, :class => 'btn btn-success whiteText' %> 

予想通りこれは、機能をリダイレクトし、受注に私を取ります。これに

<%= link_to "Checkout", new_charge_path, :class => 'btn btn-success whiteText' %> 

:だから、私はチェックアウトのリンクを交換し、このことから、それを回していて開始する

次を含むnew.html.erb

<h1>Order Information</h1> 

<br> 

<%= render 'form', order: @order %> 

<%= link_to 'Back', products_path %> 

レンダリングするフォームには次のコラde:

<%= form_for(order) do |f| %> 
    <% if order.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(order.errors.count, "error") %> prohibited this order from being saved:</h2> 

     <ul> 
     <% order.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 


    <div class="row"> 

    <div class="col-md-6"> 

     <div class="field"> 
     <%= f.label :first_name %> 
     <%= f.text_field :first_name, size: 20, :value => current_user.first_name, :class => "form-control" %> 
     </div> 

     <div class="field"> 
     <%= f.label :last_name %> 
     <%= f.text_field :last_name, size: 20, :value => current_user.last_name, :class => "form-control" %> 
     </div> 

     <div class="field"> 
     <%= f.label :address %> 
     <%= f.text_area :address, size: 40, :value => current_user.address, :class => "form-control" %> 
     </div> 

     <div class="field"> 
     <%= f.label :city %> 
     <%= f.text_area :city, size: 20, :value => current_user.city, :class => "form-control" %> 
     </div> 

     <div class="field"> 
     <%= f.label :state %> 
     <%= f.text_area :state, size: 2, :value => current_user.state, :class => "form-control" %> 
     </div> 

     <div class="field"> 
     <%= f.label :email %> 
     <%= f.text_field :email, size: 40, :value => current_user.email, :class => "form-control" %> 
     </div> 

     <div class="field"> 
     <%= f.label :pay_type %> 
     <%= f.select :pay_type, Order.pay_types.keys, prompt: 'Select a payment method', :class => "form-control" %> 
     </div> 

    </div> 



     <div class="col-md-6"> 

     <%= form_tag(payments_path, class: "form-inline") do %> 
      <%= hidden_field_tag(:purchase_amount_cents, @cart.total_price) %> 
      <div class="form_group"> 
      <%= label_tag(:credit_card_number, "Credit Card Number", class: "sr-only") %> 
      <%= text_field_tag(:credit_card_number, "", class: "form-control", placeholder: "Credit Card #") %> 
      </div> 
      <br> 
      <div class="form_group"> 
      <%= label_tag(:expiration_month, "Month", class: "sr-only") %> 
      <%= text_field_tag(:expiration_month, "", class: "form-control", placeholder: "Month") %> 
      <br> 
      <%= label_tag(:expiration_year, "Year", class: "sr-only") %> 
      <%= text_field_tag(:expiration_year, "", class: "form-control", placeholder: "Year") %> 
      <br> 
      <%= label_tag(:cvc, "Year", class: "sr-only") %> 
      <%= text_field_tag(:cvc, "", class: "form-control", placeholder: "CVC#") %> 
      </div> 
      <br> 
      <div class="form_group"> 
      <%= submit_tag("Purchase Cart", class: "btn btn-default", id: "purchase") %> 
      </div> 
     <% end %> 

     </div> 



    </div> 


    <hr> 

    <div class="actions"> 
    <%= f.submit 'Proceed to Payment' %> 
    </div> 
<% end %> 

支払いオプションは、クレジットカード(ストライプ)またはPaypalです。私は最終的にPaypal機能を追加しますが、Stripe APIは私が今持っているものすべてです。ここで

は私の注文コントローラーである:ここでは

class OrdersController < ApplicationController 
    include CurrentCart 
    before_action :set_cart, only: [:new, :create] 
    before_action :ensure_cart_isnt_empty, only: :new 
    before_action :set_order, only: [:show, :edit, :update, :destroy] 

    # GET /orders 
    # GET /orders.json 
    def index 
    @orders = Order.all 
    end 

    # GET /orders/1 
    # GET /orders/1.json 
    def show 
    end 

    # GET /orders/new 
    def new 
    @order = Order.new 
    end 

    # GET /orders/1/edit 
    def edit 
    end 

    # POST /orders 
    # POST /orders.json 
    def create 
    @order = Order.new(order_params) 
    @order.add_line_items_from_cart(@cart) 
    respond_to do |format| 
     if @order.save 
     format.html { redirect_to new_charge_path} 
     else 
     format.html { render :new } 
     format.json { render json: @order.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /orders/1 
    # PATCH/PUT /orders/1.json 
    def update 
    respond_to do |format| 
     if @order.update(order_params) 
     format.html { redirect_to @order, notice: 'Order was successfully updated.' } 
     format.json { render :show, status: :ok, location: @order } 
     else 
     format.html { render :edit } 
     format.json { render json: @order.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /orders/1 
    # DELETE /orders/1.json 
    def destroy 
    @order.destroy 
    respond_to do |format| 
     format.html { redirect_to orders_url, notice: 'Order was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_order 
     @order = Order.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def order_params 
     params.require(:order).permit(:first_name, :last_name, :address, :city, :state, :email, :pay_type) 
    end 

    def ensure_cart_isnt_empty 
     if @cart.line_items.empty? 
     redirect_to products_path, notice: 'Your cart is empty!' 
     end 
    end 

end 

は私の充電コントローラです:

class ChargesController < ApplicationController 
    include CurrentCart 
    before_action :set_cart, only: [:new, :create] 

    def new 
    end 

    def create #METHOD IS CALLED AFTER PAYMENT IS MADE 
    # Amount in cents 
    @amount = @cart.total_price 

    customer = Stripe::Customer.create(
     :email => params[:stripeEmail], 
     :source => params[:stripeToken] 
    ) 

    charge = Stripe::Charge.create(
     :customer => customer.id, 
     :amount  => @amount, 
     :description => 'Customer', 
     :currency => 'usd' 
    ) 

    Cart.destroy(session[:cart_id]) 

    rescue Stripe::CardError => e 
     flash[:error] = e.message 
     redirect_to new_charge_path 
    end 

end 

ここで問題があります。リダイレクトは正常に機能しますが、注文コントローラを使用する場合は、充電コントローラの@ amountは$ 0.00に設定されます。ただし、カートが充電コントローラに直接リンクしている場合は、正しい金額が使用されます。だから、私は何とかカートオブジェクトが失われているか、その値がリセットされていると仮定しています。ここ

def set_cart 
     @cart = Cart.find(params[:id]) 
end 

そして、私のCurrentCartモジュールです:あなたが前方値@cartを渡すに必要

module CurrentCart 
    private 
    def set_cart 
     @cart = Cart.find(session[:cart_id]) 
    rescue ActiveRecord::RecordNotFound 
     @cart = Cart.create 
     session[:cart_id] = @cart.id 
    end 
end 
+0

んストライプコールを助けることができます)一度充電が行われたら? –

+0

私はそう信じていません。 – matt

+0

'set_cart'メソッドを表示し、' CurrentCart'に何を持っていますか? – Pavan

答えて

0

私の考えは

は、ここに私のset_cart方法です。ここにあなたのリンクの内側

を行うにはいくつかのステップをコントローラを注文するカートのショーのフォームからTOTAL_PRICE(あなたがのparamsとして値を渡す)order_controller内部

<%= link_to "Checkout", new_order_path(total_price: @cart.total_price), method: :get, :class => 'btn btn-success whiteText' %> 

(あなたがのparamsを取得し、インスタンスvariabelに入れて)

# GET /orders/new 
    def new 
    @order = Order.new 
    @temp_total_price = params[:total_price] 
    end 

はあなたのform_forタグの間order_form挿入の内側に、私たちは隠された値としての形でこれを配置する必要があり、充電のためのメソッドを作成するために渡すことができ

あなたのクラスChargesController内部

あなたはTOTAL_PRICE

@total_price = params[:total_price] 

得ることができる方法を作成し、私はそれが少しログです知っているが、おそらくこれは、ウェブフックに戻って(ChargesController#のcreate` `へ

+0

残念ながら、私が試してみると、充電コントローラーに、その量が設定されていないことを示すエラーが表示されています。 – matt

+0

ビューとコントローラの間でトレースしようとすると、新しいコマンドを実行した後に、たとえば、コマンド内でorder_controllerをトレースするためにputsコマンドを使用できます。params [:total_price]がnil/0/other値であるかどうかを確認します。 – widjajayd

+0

"at" cart.total_priceを使用してメソッドを作成します。正しい金額を表示するので、注文コントローラは「at」cart.total_priceの金額に確実にアクセスできます。しかし何らかの理由で、充電コントローラはできません。 Stack Overflowでシンボルが許可されないため、このコメントで「at」を使用しています。 – matt

関連する問題