我目前正试图通过条纹在我的patform(教程的一部分)上设置付款选项。出于某种原因,当有人输入卡片详细信息并单击按钮ADD时,我在/ add_card页面上收到Nil Class错误。不知道我在这里做错了什么。
任何想法都将不胜感激!
由于
Users_Controller
def add_card
if current_user.stripe_id.blank?
customer = Stripe::Customer.create(
email: current_user.email
)
current_user.stripe_id = customer.id
current_user.save
#customer.sources.create(source: params[:stripeToken])
else
customer = Stripe::Customer.retrieve(current_user.stripe_id)
end
# Add Credit Card to Stripe
month, year = params[:expiry].split(/ \/ /)
new_token = Stripe::Token.create(:card =>{
:number => params[:number],
:exp_month => month,
:exp_year => year,
:cvc => params[:cvv]
})
customer.sources.create(source: new_token.id)
flash[:notice] = "Your card is saved."
redirect_to payment_method_path
rescue Stripe::CardError => e
flash[:alert] = e.message
redirect_to payment_method_path
end
付款方式
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">Payment Method</div>
<div class="panel-body">
<div class="container">
<div class='card' style="margin-bottom: 20px"></div>
<%= form_tag("/add_card", method: "post", id: "add-card") do %>
<div class="row pull-center">
<div class="col-md-12">
<div class="well">
<div class="row">
<div class="col-md-8 form-group">
<label>Card Number</label>
<input type="text" name="number" class="form-control">
</div>
<div class="col-md-4 form-group">
<label>Expiration</label>
<input type="text" placeholder="MM/YY" name="expriry" class="form-control">
</div>
</div>
<div class="row">
<div class="col-md-8 form-group">
<label>Name</label>
<input type="text" name="name" class="form-control">
</div>
<div class="col-md-4 form-group">
<label>CVV</label>
<input type="text" name="cvv" class="form-control">
</div>
</div>
<div class="row">
<div class="col-md-12 text right"></div>
<% if current_user.stripe_id.blank? %>
<button class="btn btn-normal btn-block">Addd Card</button>
<% else %>
<button class="btn btn-normal btn-block">Update Card</button>
<% end %>
</div>
</div>
</div>
</div>
<% end %>
答案 0 :(得分:3)
表单中expiry
字段的名称为expriry
,其中包含额外的r
。因此,您在控制器中查看的字段没有值,因此评估为nil
,并且您无法在string
对象上执行nil
方法。< / p>
在表单中将expriry
更改为expiry
。