Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabled radio button in simple_form with an array

What is the best way to place two radio button inside a simple_form, where one button is by default disabled and another is selected!

f.input_field :listing_type, as: :radio_buttons, collection: [ "lease", "sale"], :item_wrapper_class => 'inline'

this is giving me two buttons where both are enabled and selected. I want sale to be disabled and lease to be selected by default.

like image 400
geekdeepak Avatar asked Feb 04 '26 17:02

geekdeepak


1 Answers

Here's an updated answer. You can:

  • Specify which radio button is disabled by adding hash to the collection: collection: [['op1', 'val1', disabled: false], ['op2', 'val2', disabled: true]]
  • Specify which radio button is checked by either add checked to its collection hash, or add checked: value_to_be_checked to the main options hash. Here's an examples of both options:

example1:

<%= f.input_field :listing_type, as: :radio_buttons, collection: [ ['lease', 'lease', disabled: false], ['sale', 'sale', disabled: true]], checked: 'lease', :item_wrapper_class => 'inline' %>

example2:

<%= f.input_field :listing_type, as: :radio_buttons, collection: [ ['lease', 'lease', disabled: false, checked: true], ['sale', 'sale', disabled: true]], :item_wrapper_class => 'inline' %>
like image 172
Roko Avatar answered Feb 06 '26 05:02

Roko