I need to convert an integer to hours and minutes in Ruby but am having a brain freeze. I have an integer field called duration, which is set when a form is populated from hours and minutes fields on the form. This but works nicely. What Im struggling with is going the other way. I need to take a @duration in the form of an integer, and populate the form fields when editing a record.
so on the form I am doing this
= select_tag "hours", options_for_select((0..12).step(1)), :prompt => "hours", :include_blank => "#{@hours}",  required: true
The hours in this example is coming from the controller, which does
@hours = (@log.duration / 60).to_i
My minutes field on the form is this...
= select_tag "minutes", options_for_select((0..60).step(1)), :prompt => "Minutes", :include_blank => "#{@minutes}",  required: true
and here is where I am struggling. I need to generate the @minutes variable in the controller, but can't think my way through how to get to it. I guess what I need to do is know the remainder left over when the @log.duration is divided by 60, but how would I get to this?
Thanks
The answer was the modulo operator
@minutes = (@log.duration % 60).to_i
Simple and does the job perfectly. Sorry for wasting your time.
G
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With