want rails simple_form radio button to display text that's not the value
Asked Answered
V

2

12

I am using the simple_form gem https://github.com/plataformatec/simple_form to create some input fields; one of which is a radio button group, like this:

<%= f.input :due_date, :collection => ['Today', 'Tomorrow', 'In 3 Days'], :as => :radio %>

So this field in the database "due_date" is a Date. But rather than letting people click on that smallish calendar, we know that generally people just want these three options. And I do want to use radio button. But the output of this line suggest that if I hit the submit button now, the due_date param will have the values stated there, i.e., Today, Tomorrow or In 3 Days. Here's the output HTML for the Today part.

<span>
<input class="radio optional" id="project_due_date_today" name="project[due_date]" type="radio" value="Today">
<label class="collection_radio" for="project_due_date_today">Today</label>
</span>

What I want ideally is like :

<%= f.input :due_date, :collection_to_params => [Date.today, Date.tomorrow, Date.today+3], :display_value => ['Today', 'Tomorrow', 'In 3 Days'], :as => :radio %>

So when a user clicks on one, and submits, I actually get some ISO date sent to the server.

Any ideas?

Thanks!

Vacillation answered 21/9, 2011 at 10:4 Comment(3)
I haven't used simple_form, but usually with Rails it's worth trying a hash: :collection => {'Today' => Date.today, 'Tomorrow' => Date.tomorrow, 'In 3 Days' => Date.today+3}Megasporangium
Hey Alex, thanks for the input, I will keep that in mind, but I'd found the simple for answer.Vacillation
Small notice, that there are radio is deprecated, and replaced to a radio_buttons.Magnific
V
31
<%= f.input :due_date, 
:collection => [[Date.today, 'Today'], [Date.tomorrow, 'Tomorrow'], [Date.today+3, 'In 3 Days']], 
:label_method => :last, 
:value_method => :first, 
:as => :radio_buttons %>

So you can pass either an array of arrays to the collection hash, and then the there are two more options that can be passed to specify the VALUE of this input and the visible label of this input and they are properly named label_method and value_method. the word method being, what method to call on the individual item of the collection in question to retrieve the label or the value. In the array of array's case, array.first is my value, array.last if my label. Hence the code above

Vacillation answered 21/9, 2011 at 11:7 Comment(4)
Why not use ['Today', Date.today] without unnecessary params like label_method, value_method which is default to first and last respectively?!Voguish
In case it's not obvious what Olexandr says, just reverse your order of each collection entry (i.e. ['Today', Date.today] instead of [Date.today, 'Today']) and now you don't need to specify the :label_method and :value_method.Crotchet
You could also write: <%= f.collection_radio_buttons :due_date, [[Date.today, 'Today'], [Date.tomorrow, 'Tomorrow'], [Date.today+3, 'In 3 Days']], :first, :last %>Craigie
now you need to use the new syntax also as: :radio_buttonsHammel
R
2

This is what I've got from Nik So's answer with all comments applied:

<%= f.input :due_date, 
collection: [['Today', Date.today], ['Tomorrow', Date.tomorrow], ['In 3 Days', Date.today+3]],
as: :radio_buttons %>
Rockingham answered 18/4, 2016 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.