2015-12-24 98 views
1

我正在尝试设置一个简单的购物车。 我按照教程设置了购物车,并试图将其与Braintree连接起来。 购物车功能正常,Braintree支付也可以使用(如果我输入一个数字作为金额),但我在将购物车总价格变量发送到交易控制器时遇到问题。 我迄今为止代码:Ruby on Rails。将视图中的变量传递给交易控制器

车的index.html

<div class="cart-item-container"> 
<%= link_to 'Empty your cart', cart_clear_path %> 
<br> 
<br> 
<% tokentotal = 0 %> 
<% total = 0 %> 
<ul> <% @cart.each do | id, quantity | %> 
    <% product = Product.find_by_id(id) %> 
<% if quantity != 0 %> 
    <li class="cart-items"> 
    <%= link_to product.title, product %> 
    <p><%= product.description %></p> 
    <p><%= number_to_currency(product.price, :unit => '€') %></p> 
    <p>Quantity<%= quantity %></p> 
    <a href="/cart/remove/<%= product.id %>"> Remove from cart</a> 
    </li> 

    <% total += quantity * product.price %> 
    <% tokentotal += quantity * product.tokens %> 

    <p>Total <%= number_to_currency(total, :unit => '€') %></p> 
    <p> Tokens <%= tokentotal %></p> 

</ul> 
<% end %> <%end%> 
</div> 

<form id="checkout" method="post" action="/transactions"> 
    <div id="payment-form"></div> 
    <%= form_tag transactions_path do%> 
    <%# render 'customer_form' unless current_user.has_payment_info? %> 
    <div id="dropin"> <p>Please enter your payment details:</p> 
</div> 
    <%=submit_tag "Pay", class: "button mt1" %> 
    <% end %> 

</form> 

<script src="https://js.braintreegateway.com/v2/braintree.js"></script> 
<script type="text/javascript"> 
    function setupBT() { 
    braintree.setup("<%[email protected]_token%>", 'dropin', { 
     container: 'dropin' 
    }); 
    } 
    if (window.addEventListener) 
    window.addEventListener("load", setupBT, false); 
    else if (window.attachEvent) 
    window.attachEvent("onload", setupBT); 
    else window.onload = setupBT; 
    </script> 
</script> 

这将显示在购物车中节奏的物品的总价。我想要一种方式将<%total%>传递给交易控制器的“金额”部分。我尝试了很多变化,但在这个例子中留下了空白。

交易控制器:

class TransactionsController < ApplicationController 
before_action :authenticate_user! 


    def new 
end 

def create 
    unless current_user.has_payment_info? 
    @result = Braintree::Transaction.sale(
       amount: , 
       payment_method_nonce: params[:payment_method_nonce], 
       customer: { 
        first_name: params[:first_name], 
        last_name: params[:last_name], 
        company: params[:company], 
        email: current_user.email, 
        phone: params[:phone] 
       }, 
       options: { 
        store_in_vault: true 
       }) 
    else 
    @result = Braintree::Transaction.sale(
       amount: , 
       payment_method_nonce: params[:payment_method_nonce]) 
    end 


    if @result.success? 
    session[:cart] = nil 
    Token.create(user_id: current_user.id) 
    redirect_to new_gig_path, notice: "Congraulations! Your transaction has been successfully!" 
    else 
    flash[:alert] = "Something went wrong while processing your transaction. Please try again!" 
    render :new 
    end 
end 


private 


def token_params 
    params.require(:token).permit(:user_id) 
end 
private 
def generate_client_token 
    if current_user.has_payment_info? 
    Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id) 
    else 
    Braintree::ClientToken.generate 
    end 
end 
end 

的车控制:

class CartController < ApplicationController 
    before_action :authenticate_user! 
    before_action :find_product 


def remove 
    id = params[:id] 
    if session[:cart] then 
    cart = session[:cart] 
    else 
    session[:cart] = {} 
    cart = session[:cart] 
    end 
    if cart[id] then 
    cart[id] = cart[id] - 1 
    else 
    cart[id] = 1 
end 
redirect_to :action => :index 
end 

    def add 
    id = params[:id] 
    #if cart already created use exisiing one 
    if session[:cart] then 
     cart = session[:cart] 
    else 
     session[:cart] = {} 
     cart = session[:cart] 
    end 
    #if token already in cart increase value 
    if cart[id] then 
     cart[id] = cart[id] + 1 
    else 
     cart[id] = 1 
    end 
    redirect_to :action => :index 
    end #end add method 

    def clearCart 
    session[:cart] = nil 
    redirect_to :action => :index 
    end 



    def index 
    if current_user.has_payment_info? 
    @client_token = Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id) 
    else 
    @client_token = Braintree::ClientToken.generate 
    end 
    #if there is a cart, pass it to the page for display else pass empty value 
    if session[:cart] then 
     @cart = session[:cart] 
    else 
     @cart = {} 
    end 
end 


private 

def find_product 
@product = Product.find_by_id(params[:id]) 
end 

end 

感谢您寻找。

回答

3

您可以使用表单中的隐藏字段将金额发送到控制器。

<form id="checkout" method="post" action="/transactions"> 
    <div id="payment-form"></div> 
    <%= form_tag transactions_path do%> 
    <%= hidden_field_tag 'amount', total %> 
    <%# render 'customer_form' unless current_user.has_payment_info? %> 
    <div id="dropin"> <p>Please enter your payment details:</p> 
</div> 
    <%=submit_tag "Pay", class: "button mt1" %> 
    <% end %> 

</form> 
+0

太简单了。这工作得很好,谢谢。 (向控制器添加params [:amount])。 –

0

如果你想@archana的回答更加简洁,你可以帕拉姆传递到button_to帮手:

<%= button_to "Pay", transaction_path, params: { amount: total } %> 

这将创建与amount隐藏的输入形式,传递值给控制器。

相关问题