1
我實際上在我的Rails項目中使用了表單助手radio_button
。如果一切正常,代碼本身不會對我來說很好:Rails radio_button重構最佳實踐
_form.html.haml
#- Loop on durations types
- Product::DURATIONS.each_with_index do |name, index|
#- If new record, then select the first index by default
- unless @product.duration_type.present?
- checked = (index == 0) ? true : false
- else
#- Otherwise, if edit, then select the product value
- checked = (name == @product.duration_type) ? true : false
= f.radio_button :duration_type, name, checked: checked
= f.label :duration_type, name
product.rb
DURATIONS = %w(Hour Day Week Month Year)
有沒有更好的辦法以更乾和Rails的方式寫這篇文章?
非常感謝
真棒哥們,謝謝:) – lkartono
,因爲在它的一個錯誤改變了第二種方法。 – tihom