dan
dan

Reputation: 1080

Rails - Can you use collection_radio_buttons with options_for_select?

I currently have a select in a form which works fine:

<%= f.select(:scan_type, options_for_select(Scan::SCAN_TYPES, task.scan_type)) %>

I want to convert it to a set of radio buttons, as there are only a few options. Is there a way to use options_for_select with collection_radio_buttons?

I'm just using a simple array for my options, i.e. in scan.rb -

SCAN_TYPES = ['roll', 'single']

My first approach was to try

<%= f.collection_radio_buttons(:scan_type, options_for_select(Scan::SCAN_TYPES, object.scan_type)) %>

But I'm not providing all the arguments. I'm at a loss to see what needs to be added.

Upvotes: 0

Views: 428

Answers (1)

Vrushali Pawar
Vrushali Pawar

Reputation: 3803

f.collection_radio_buttons(:scan_type, Scan::SCAN_TYPES.map{|s| [s, s] }, checked: f.object.scan_type)

Check this

Upvotes: 1

Related Questions