繁体   English   中英

Ruby on Rails。 将变量从视图传递到事务控制器

[英]Ruby on Rails. Pass variable from views to transaction controller

我正在尝试设置一个简单的购物车。 我已经按照教程设置了购物车,我正在尝试将其连接到Braintree。 购物车功能有效,Braintree支付工作(如果我输入一个数字作为金额),但我遇到问题将购物车总价格变量发送到交易控制器。 我的代码到目前为止:

购物车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("<%=@client_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%>传递给事务控制器的'amount'部分。 我尝试了很多变化,但在这个例子中留下了空白。

交易控制器:

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

谢谢你的期待。

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

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

如果您希望@archana的答案更简洁,您可以将param传递给button_to帮助器:

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

这将创建一个带有隐藏输入amount的表单,将值传递给控制器​​。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM