Rails /单选按钮,禁用标签和选项中的图像

时间:2014-10-20 21:29:27

标签: ruby-on-rails-4 radio-button simple-form

Rails 4.我试图创建一个单选按钮

1)文字加图片标签

2)可以(或不禁用)选项

我可以用f.collection_radio_buttons来解决1){2}与f.input ......但是我找不到解决方案2点......

有一个解决方案只使用简单的表单助手吗? (没有js / Jquery)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我会尽力回答:

此代码解决了2点:

 <%= f.collection_radio_buttons( :typepayment, 
                                 [[0,t(:radio_button_type_payment_None)], [1,t(:radio_button_type_payment_DirectDebit)], [2,t(:radio_button_type_payment_CreditCard)]],
                                 :first,
                                 :last,
                                 item_wrapper_tag: :div
                               ) do |b|
                                   b.label {  (condition_is_disabled ? b.radio_button(disabled: "\"disabled\"") : b.radio_button) + " "+
                                             (b.value == 0 ? t(:radio_button_type_payment_None) : (b.value == 1 ? t(:radio_button_type_payment_DirectDebit) : t(:radio_button_type_payment_CreditCard) )) +
                                             (b.value != 0 ? image_tag("typepayment_#{b.value}.png", class: "img_type_payment") : "") }
                                 end

 %>

collection_radio_buttons的块代码用于构建带图像的labee。

disabled: "\"disabled\"":我没有找到将disabled状态传递给帮助者的其他方法

如果有人有更好的答案,欢迎:)