使用数组禁用 simple_form 中的单选按钮

gee*_*pak 2 ruby ruby-on-rails ruby-on-rails-3 simple-form

在 simple_form 中放置两个单选按钮的最佳方法是什么,其中一个按钮默认禁用,另一个按钮被选中!

f.input_field :listing_type, as: :radio_buttons, collection: [ "lease", "sale"], :item_wrapper_class => 'inline'
Run Code Online (Sandbox Code Playgroud)

这给了我两个按钮,两个按钮都被启用和选择。我希望禁用销售并默认选择租赁。

Rok*_*oko 5

这是更新的答案。你可以:

  • 通过将哈希添加到集合来指定禁用哪个单选按钮:collection: [['op1', 'val1', disabled: false], ['op2', 'val2', disabled: true]]
  • 通过将检查添加到其集合哈希或添加checked: value_to_be_checked到主选项哈希来指定检查哪个单选按钮。以下是这两个选项的示例:

示例1:

<%= f.input_field :listing_type, as: :radio_buttons, collection: [ ['lease', 'lease', disabled: false], ['sale', 'sale', disabled: true]], checked: 'lease', :item_wrapper_class => 'inline' %>
Run Code Online (Sandbox Code Playgroud)

示例2:

<%= f.input_field :listing_type, as: :radio_buttons, collection: [ ['lease', 'lease', disabled: false, checked: true], ['sale', 'sale', disabled: true]], :item_wrapper_class => 'inline' %>
Run Code Online (Sandbox Code Playgroud)