I’m using Rails 4.2.3. In my model, I have a field that records the amount of time it took someone to do something. It is a field called “time_in_ms,” which is an integer in my PostGresql database. My question is, in my form, I don’t want the user to have to enter in milliseconds, I would rather have them select from hours, minutes, and seconds. How do I set this up in my Rails form? Since there are no such fields in my model, I don’t know what to put in my “collection_select” attributes …
<div class="field">
<%= f.label "Time" %>
<%= collection_select(:time_unit, :time_unit, @hours, {:prompt => true} ) %> hrs
<%= collection_select(:time_unit, :time_unit, @minutes, {:prompt => true} ) %> min
<%= collection_select(:time_unit, :time_unit, @seconds, {:prompt => true} ) %> sec
<%= f.hidden_field :time_in_ms, :validate => true %>
</div>
How do I properly write and pre-populate the selectors based on my single “time_in_ms” field in my model?
Thanks, - Dave
As you haven't mentioned your controller, I am assuming the controller as TimersController. You need to make changes in the view as well as timer_params method in the controller.
Here is the code on view:
and here is the code on controller:
I have left the validation part. You can implement that on client side as well as on server side.