Reputation: 253
I have a form to record sports times : I need minutes, seconds and hundredths of second, i.e. 1:31.43 --- I don't need the hour. In my form I use :
<%= f.time_select :perf_time, {:discard_hour => true, :default => {:minute => '00', :second => '00'}, :include_seconds => true} %>
This displays 2 select pull-downs, one for minutes and one for seconds.
I have added a separate field for hundredths of second (type Integer):
<%= f.number_field :perf_time_cents, :in => 0..999 %>
Now I'd like to use the method .change()
in my helper to change add/change the microseconds to perf_time. Here's my code, but it does not do anything.
before_save :set_perf_time
def set_perf_time
self.perf_time.change(usec: (self.perf_time_cents * 10))
end
Upvotes: 0
Views: 270
Reputation: 24350
There is no option for milliseconds in time_select
because a combobox with 1000 possible values would not be user friendly.
You could use instead a separate text field for the number of milliseconds, with the HTML5 type number
:
<%= f.number_field :perf_time_millis, :in => 0..999 %>
In the controller, use both the values in the selects and the text input field to get the full time in millis:
time_in_millis = params[:perf_time_millis].to_i + 1000 * (params["perf_time(5i)"].to_i * 60 + params["perf_time(6i)"].to_i))
Upvotes: 1