我使用更新订阅的表单,并允许用户在文件中使用当前的信用卡,或使用其他信用卡。我使用了两个单选按钮,允许他们传入当前保存的内容或传递新的表单数据。
我认为的相关代码(HAML):
= form_for(@user) do |f|
#payment-option
=f.radio_button :credit_card_attributes, id: 'old_cc_number', checked: true
# not sure what to put here
="Use your current card ending in #{current_user.subscription.credit_card.masked_card_number.slice 15..19}"
%br
#payment-option
=f.radio_button :credit_card_attributes, current_user.subscription.credit_card.masked_card_number, id: 'new_cc_number'
Enter new payment information
.signup-field-container.new-payment-information{style:'display:none'}
= f.text_field :credit_card_number, placeholder: "Card Number", class: ' signup-fields card-number', id: 'credit_card_input'
= f.select(:expiration_month, ['01', '02', '03', '04', '05', '06', '07','08','09','10','11','12'], {prompt: 'MONTH'}, class: 'signup-fields exp-date')
= f.select(:expiration_year, ['2015', '2016', '2017', '2018', '2019', '2020', '2021','2022','2023','2024','2025','2026'], {prompt: 'YEAR'}, class: 'signup-fields exp-date')
= f.number_field :cvv, placeholder: "CVV", class: ' signup-fields cvv'
目标是使用JSON向外部API发出请求。
current_user.subscription.update_attributes(
{
"credit_card_attributes"=>
{
"full_number" => credit_card_number,
"cvv" => cvv,
"expiration_month" => expiration_month,
"expiration_year" => expiration_year
}
}
)
有关如何构建单选按钮的任何建议,以便我可以使用当前保存的第一个单选按钮的信用卡值传入参数,并为第二个单选按钮传入带有新信用卡值的参数?我知道当前的表单结构完全混乱。我怀疑它是一个简单的解决方案,但我对单选按钮的经验不足让我挠头。
提前致谢。
答案 0 :(得分:0)
=f.radio_button :credit_card_attributes, id: 'old_cc_number', value: @old_cc_number checked: true
使用值应该有效...但我认为你不应该将cc信息传递给视图。
我会像这样设置收音机:
= form_for(@user) do |f|
#payment-option
=f.radio_button :use_old_cc_number, id: 'old_cc_number', value: true, checked: true
# not sure what to put here
="Use your current card ending in #{current_user.subscription.credit_card.last_four}"
%br
#payment-option
=f.radio_button :use_old_cc_number, current_user.subscription.credit_card.masked_card_number, id: 'new_cc_number', value: false
Enter new payment information
...
使用单选按钮名称" use_old_cc_number"以及设置控制器逻辑的值true / false:
def your_controller_method
...some logic...
if params[:use_old_cc_number] == true
full_number = @current_user.subscription.credit_card
else
full_number = params[:credit_card_number]
end
...make json call...
end
这应该有用。