1

In my Rails form I have a field for selecting a person's rate/hour. I would like this to display in my form as a collection_select dropdown with select options of the format:

$1.00 per hour

$1.25 per hour

$1.50 per hour

etc.

Currently I have a rate field t.decimal "rate"

And my form (I'm using simple form) that currently just displays numbers with two decimal places:

<%= f.input :rate, label:false, collection: (0..100).step(0.25).map{ |n| number_with_precision(n, precision: 2) }, input_html: {class: "rate-select", min: '0'}  %>

What would be my best way of displaying the select dropdown in my desired format above with currency, and 'per hour' surrounding the rate but only saving the rate as a decimal? For now the currency doesn't need to be selected or stored so basically I'm just looking to extract the numerical portion of the select options.

Brett
  • 162
  • 13

1 Answers1

0

You can do so by passing a 2D array to collection option. E.g.

collection: [['$1.00 per hour', 1.00], ['$1.25 per hour', 1.25]]

Where each subarray represents the text and value of an option, accordingly.

Some ol' docs: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select

Petr Gazarov
  • 3,602
  • 2
  • 20
  • 37