2017-07-12 14 views
0

私はこの質問に正しく慣れているかどうか分かりません。私は、Harry PotterテーマレールオンラインストアのデモアプリケーションにStripe APIを使用しています。私はStripeボイラープレートのコードに従っています。そのため、現在、ハードコードされた値$ 1.00で設定されています。ショッピングカートには、カート内の全商品の総コストを表示する方法があります。それはうまくいきますが、その値を料金コントロールに渡す方法がわからないので、それを支払い額として設定します。Railsで他のクラスから価値を引き出す方法

私はRailsにはかなり新しいので、参考になる説明があれば大歓迎です。

ここ
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ファイルです:

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

は私の料金コントローラである:ここで

は私料/ new.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> 

は私のカートのコントローラであります0

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 

ここでは私のカートモデルである:あなたのcharges_controllerで

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

ページにレンダリングされる作業ボタンがあります。ストライプAPIを使用してフォームを作成しています。 – matt

+0

この$ 1は、あなたがすでに提供している 'data-amount'属性とは異なる何かを指していますか?なぜ、 'data-amount = '1'? –

答えて

1

、金額を変更します:

@amount = @cart.total_price * 735 

そして、あなたの費用

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 

そしてここでは、私のルートファイルです/new.html.erb:

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

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

ことができますなら、私に教えてください。

関連する問題