【发布时间】:2020-11-13 14:15:02
【问题描述】:
我正在我的 rails 应用程序中构建一个自定义结帐表单,并在发布到控制器时收到此错误:
“无效的源对象:必须是字典或非空字符串”
这是我的表格:
<script src="https://checkout.stripe.com/checkout.js"></script>
<%= form_tag registrations_path, :id=>"stripeForm" do |f| %>
<h2>Register for the <%= @workshop.name %> Workshop ($<%= @workshop.price %>)</h2>
<div class="row">
<div class="form-group col-sm-6">
<%= text_field_tag(:f_name, nil, :placeholder=>"First Name", :class=>"form-control") %>
</div>
<div class="form-group col-sm-6">
<%= text_field_tag(:l_name, nil, :placeholder=>"Last Name", :class=>"form-control") %>
</div>
</div>
<div class="row text-center">
<button class="buy-button" id="stripe-button">Buy Ticket</button>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<%= hidden_field_tag 'stripeAmount' %>
</div>
<script>
var handler = StripeCheckout.configure({
key: "<%= ENV['stripe_publishable_key'] %>",
token: function (token, args) {
$("#stripeToken").value = token.id;
$("#stripeEmail").value = token.email;
$("#stripeAmount").value = <%= @workshop.price * 100 %>;
$("#stripeForm").submit();
}
});
$('#stripe-button').on('click', function (e) {
// Open Checkout with further options
$name = $('input[name=f_name]').val() + " " + $('input[name=l_name]').val();
handler.open({
name: $name,
description: "<%= @workshop.name %>" + " workshop",
amount: <%= @workshop.price * 100 %>
});
e.preventDefault();
});
$(window).on('popstate', function() {
handler.close();
});
</script>
<% end %>
这是我的控制器操作:
def create
# Amount in cents
amount = params[:stripeAmount].to_i * 100
# Create the customer in Stripe
customer = Stripe::Customer.create(
email: params[:stripeEmail],
card: params[:stripeToken]
)
# Create the charge using the customer data returned by Stripe API
charge = Stripe::Charge.create(
customer: customer.id,
amount: amount,
description: 'Rails Stripe customer',
currency: 'usd'
)
# place more code upon successfully creating the charge
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
flash[:notice] = "Please try again"
end
基本上用户填写名字和姓氏,点击支付按钮。然后他们填写条带信息并提交。我试过在他们点击提交后立即使用调试器,所有的令牌信息等都在那里。
但是,一旦它到达控制器中的创建操作,我就会收到一个错误,并显示所有条带参数的空字符串。
我哪里出错了?
编辑:将以下强参数添加到控制器但没有效果:
protected
def registration_params
params.require(:registration).permit(:stripeEmail, :stripeToken, :stripeAmount)
end
【问题讨论】:
-
您使用的是什么版本的 Rails?如果 Rails 4+,你应该从帮助函数中的
params散列中列出你需要的东西。 -
嗯.. 所以我将 strong_params 添加到我的控制器并得到完全相同的错误(并且参数仍然为空)
标签: ruby-on-rails stripe-payments