2017-07-12 66 views
0

我不知道我是否正確解答了這個問題。我使用的是哈利波特主題的Rails在線商店演示應用程序的Stripe API。我遵循條紋樣板代碼,所以我現在的硬編碼值爲1.00美元。在我的購物車中,有一種方法可以顯示購物車中所有物品的總成本。這工作正常,但我不知道如何將該值傳遞給收費控制器,以便將其設置爲付款金額。如何從Rails中的其他課程中獲取價值

我是相當新的Rails,所以任何有用的解釋將不勝感激。

這是我費/ new.html.erb文件:

<%= form_tag charges_path do %> 
    <article> 
    <% if flash[:error].present? %> 
     <div id="error_explanation"> 
     <p><%= flash[:error] %></p> 
     </div> 
    <% end %> 
    <label class="amount"> 
     <span>Amount: $1.00</span> 
    </label> 
    </article> 

    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
      data-key="<%= Rails.configuration.stripe[:publishable_key] %>" 
      data-description="A month's subscription" 
      data-amount="100" 
      data-locale="auto"></script> 
<% 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 = 100 

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

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

    Cart.destroy(session[:cart_id]) 

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

end 

這是我的車/ show.html.erb文件:

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

這是我的購物車控制器:

class CartsController < ApplicationController 
    before_action :set_cart, only: [:show, :edit, :update, :destroy] 
    rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart 

    # GET /carts 
    # GET /carts.json 
    def index 
    @carts = Cart.all 
    end 

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

    # GET /carts/new 
    def new 
    @cart = Cart.new 
    end 

    # GET /carts/1/edit 
    def edit 
    end 

    # POST /carts 
    # POST /carts.json 
    def create 
    @cart = Cart.new(cart_params) 

    respond_to do |format| 
     if @cart.save 
     format.html { redirect_to @cart, notice: 'Cart was successfully created.' } 
     format.json { render :show, status: :created, location: @cart } 
     else 
     format.html { render :new } 
     format.json { render json: @cart.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

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

    # DELETE /carts/1 
    # DELETE /carts/1.json 
    def destroy 
    @cart.destroy if @cart.id == session[:cart_id] 
    session[:cart_id] = nil 
    respond_to do |format| 
     format.html { redirect_to root_path, notice: 'Cart was emptied.' } 
     format.json { head :no_content } 
    end 
    end 

    def update_quantity 
    @line_item.update_attribute(:quantity) 
    end 

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

    # Never trust parameters from the scary internet, only allow the white list through. 
    def cart_params 
     params.fetch(:cart, {}) 
    end 

    def invalid_cart 
     logger.error "Attempt to access invalid cart #{params[:id]}" 
     redirect_to root_path, notice: 'Invalid cart' 
    end 
end 

這是我的車型號:

class Cart < ApplicationRecord 
    has_many :line_items, dependent: :destroy 

    def add_product(product) 
     current_item = line_items.find_by(product_id: product.id) 
     if current_item 
      current_item.quantity += 1 
     else 
      current_item = line_items.build(product_id: product.id) 
     end 
     current_item 
    end 

    def total_price 
     line_items.to_a.sum {|item| item.total_price} 
    end 

    def convert_to_muggle(galleons) 
     line_items.to_a.sum {|item| item.convert_to_muggle} 
    end  

end 

,這裏是我的路線文件:

Rails.application.routes.draw do 


    resources :charges 
    resources :orders 
    resources :line_items 
    resources :carts 
    devise_for :users 
    match 'users/:id' => 'users#destroy', :via => :delete, :as => :admin_destroy_user 
    resources :users, only: [:index, :show, :edit, :update] 

    root 'products#index' 

    resources :products 

    controller :products do 
    post '/products/destroy' => 'products#destroy', as: :destroy 
    get '/products/add_to_cart' => 'products#add_to_cart', as: :add_to_cart 
    get '/products/remove_from_cart' => 'products#remove_from_cart', as: :remove_from_cart 
    end 

    controller :line_items do 
    post '/line_items/increase_quantity' => 'line_items#increase_quantity', as: :increase_quantity 
    end 

    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
end 
+0

您的費用/ new.html.erb有點奇怪。爲什麼有一個form_tag但沒有提交按鈕? –

+0

有一個工作按鈕會呈現給頁面。它使用Stripe API創建表單。 – matt

+0

這個$ 1指的是與你已經提供的'data-amount'屬性不同的東西嗎?爲什麼不'data-amount ='1''? –

回答

1

在你charges_controller,改變用量:

@amount = @cart.total_price * 735 

而且您的費用/new.html.erb:

<span>Amount: <%= number_to_currency(@cart.total_price * 7.35) %></span> 

data-amount="<%= @cart.total_price * 735 %>" 

讓我知道是否有幫助。

相關問題