2015-04-27 70 views
0

我在循環中創建了單選按鈕。Rails radio_button在循環中有條件地檢查

- @annual_packages.each do |plan| 
    = f.radio_button('plan_id', plan.id) 
    = plan.title 

我怎樣才能使病情檢查無線電是否plan.id == @ plan.id

不起作用:

= f.radio_button('plan_id', plan.id, checked: (plan.id == @plan.id)) 

循環代碼:

= form_for @organization, url: subscription_create_path, remote: true, method: :post do |f| 
    - unless @annual_packages.blank? 
    - @annual_packages.each do |plan| 
     = f.radio_button('plan_id', plan.id) 
     = plan.title 
+0

你正在嘗試做什麼?你是否試圖將計劃標題顯示爲單選按鈕的標記並接受表單對象的plan_id?什麼是@plan? –

回答

0

如果您正在使用simple_formform_for,f爲表單對象,那麼你可以簡單地做:

爲的form_for: 你的情況radio_button(object_name, method, tag_value, options = {})refer

- @annual_packages.each do |plan| 
    radio_button("your object name", "plan_id", plan.id, options = {}) 

如果方法(object.plan_id)的當前值是tag_value(plan),則將檢查單選按鈕。

爲simple_form:

= f.input :plan_id, as: :radio_buttons, collection: @annual_packages 
+0

已更新的問題 – mxgoncharov