我正在建立一个市场,并有一个表格来收集卖家的银行账户详细信息,以便我们转账付款。
其中一个条带参数是卖家名称,它是表单上的输入。当我提交表单时,控制器只有在已经存储在数据库中时才读取名称(即如果我在db中手动输入名称,则运行表单)。如果名称尚不存在,则会给出错误,说我需要输入名称来创建条带标记。
如何更改表单/方法,以便在用户点击提交时该名称可用于条带?
以下是我的更新方法:错误与:name =>一致current_user.bankaccname 即可。这是下面粘贴的表单中的输入。
def update
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]
recipient = Stripe::Recipient.create(
:name => current_user.bankaccname,
:type => "individual",
:bank_account => token
)
current_user.recipient = recipient.id
current_user.save
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to edit_user_url, notice: 'Your account was successfully updated.' }
else
format.html { render action: 'edit' }
end
end
end
我的表格:
<%= form_for @user, url: user_path, html: { method: :put } do |f| %>
<div class="form-group">
<%= f.label :name %><i> (as it appears in your bank account)</i>
<%= f.text_field :bankaccname, class:"form-control" %>
</div>
<div class="form-group">
<%= label_tag :country %>
<%= text_field_tag :country, nil, { :name => nil, :'data-stripe' => "country", class: "form-control" } %>
</div>
<div class="form-group">
<%= label_tag :routing_number %>
<%= text_field_tag :routing_number, nil, { :name => nil, :'data-stripe' => "routingNumber", class: "form-control" } %>
</div>
<div class="form-group">
<%= label_tag :account_number %>
<%= text_field_tag :account_number, nil, { :name => nil, :'data-stripe' => "accountNumber", class: "form-control" } %>
</div>
<div class="form-group">
<%= f.submit "Submit", class:"btn btn-primary" %>
</div>
<% end %>
答案 0 :(得分:1)
检查一下,它更优化,它只对db进行一次调用(我猜)。 它也将解决你的上述问题。
def update
@user.attributes = user_params
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]
recipient = Stripe::Recipient.create(
:name => @user.bankaccname,
:type => "individual",
:bank_account => token
)
@user.recipient = recipient.id
respond_to do |format|
if @user.save
format.html { redirect_to edit_user_url, notice: 'Your account was successfully updated.' }
else
format.html { render action: 'edit' }
end
end
end