Ruby on rails RubyonRails。将变量从视图传递到事务控制器
我正在试着设置一个简单的购物车。 我已经按照一个教程设置了购物车,我正在尝试将其连接到Braintree。 购物车功能正常,Braintree付款也正常(如果我输入一个数字作为金额),但我在将购物车总价变量发送到交易控制器时遇到问题。 到目前为止,我的代码是: 购物车索引.htmlRuby on rails RubyonRails。将变量从视图传递到事务控制器,ruby-on-rails,view,transactions,controller,cart,Ruby On Rails,View,Transactions,Controller,Cart,我正在试着设置一个简单的购物车。 我已经按照一个教程设置了购物车,我正在尝试将其连接到Braintree。 购物车功能正常,Braintree付款也正常(如果我输入一个数字作为金额),但我在将购物车总价变量发送到交易控制器时遇到问题。 到目前为止,我的代码是: 购物车索引.html <div class="cart-item-container"> <%= link_to 'Empty your cart', cart_clear_path %> <br> &
<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>
-
“€”)>
数量
总额(欧元)%>
代币
请输入您的付款详细信息:
函数setupBT(){
braintree.setup(“,”dropin“{
容器:“dropin”
});
}
if(window.addEventListener)
window.addEventListener(“加载”,setupBT,false);
else if(窗口附件)
窗口。附件(“加载”,设置BT);
else window.onload=setupBT;
这将显示购物车中按步调排列的项目的总价。我想要一种方法来传递到事务控制器的“金额”部分。我尝试了许多变体,但在本例中为空
事务控制器:
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
class TransactionController
购物车控制器:
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
class-CartController:index
结束
def添加
id=params[:id]
#如果购物车已创建,请使用现有购物车
如果会话[:购物车],则
购物车=会话[:购物车]
其他的
会话[:购物车]={}
购物车=会话[:购物车]
结束
#如果令牌已在购物车中,则增加值
如果是购物车[id],则
购物车[id]=购物车[id]+1
其他的
购物车[id]=1
结束
将_重定向到:action=>:index
结束#结束添加方法
def clearCart
会话[:购物车]=无
将_重定向到:action=>:index
结束
def索引
如果当前用户有付款信息?
@client\u token=Braintree::ClientToken.generate(客户\u id:current\u user.Braintree\u客户\u id)
其他的
@client_token=Braintree::ClientToken.generate
结束
#如果有购物车,将其传递到页面以显示,否则传递空值
如果会话[:购物车],则
@购物车=会话[:购物车]
其他的
@购物车={}
结束
结束
私有的
def find_产品
@product=product.find\u by_id(参数[:id])
结束
结束
谢谢查看。您可以在表单中使用隐藏字段将金额发送给控制器
<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
的答案更简洁,可以将参数传递给助手:
这将创建一个带有
amount
隐藏输入的表单,将值传递给控制器。太简单了。这很有效,谢谢。(将参数[:amount]添加到控制器)。
<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>