Search code examples
jqueryruby-on-railssimple-form

hide empty date value for jquery.timepicker on Rails 4 on edit form


I am using this JQuery Timepicker by John Thornton and it works great as a dropdown on a "new" form (meaning the dropdown populates the input field with the value "09:00 AM".

HOWEVER, when I render the EDIT form and the time_start or time_end column has a value, the input field adds the date and looks like this "2000-01-01 09:00:00 UTC". I can click on "09:00 AM" on the dropdown to get it to change in the input, but I'd like the existing time_start or time_end value to render in the input field WITHOUT the date. It's very strange.

I am using rails 4.2 and simple_form and my db field is a time field, not a datetime.

Here is my rails snippet:

project/edit.html.slim

 #newProjectTimeMenu
        .row
          .col-md-4.col-sm-6.col-xs-6
            = f.label :time_start, label: 'Time Start'
            .input-group.date
              span.input-group-addon
                i.fa.fa-clock-o
              =  f.text_field   :time_start, required: true, class: 'form-control time-end'
         
          .col-md-4.col-sm-4.col-xs-6.pr0
            = f.label :time_end, label: 'Time End'
            .input-group.date
              span.input-group-addon
                i.fa.fa-clock-o
              =  f.text_field   :time_end, required: true, class: 'form-control time-end'

here is the jquery I'm calling on the same page:

  $('#newProjectTimeMenu .time-start, #newProjectTimeMenu .time-end').timepicker({
      'minuteStep': 30,
      'minTime': "09:00:00",
      'maxTime': "22:00:00",

      'defaultTime':  true,,
      });

WORKING SOLUTION: Thanks to the answer below, I have the solution...but I had to translate it into "simple_form" speak. Here is the code I am using:

  =  f.text_field   :time_end, required: true, class: 'form-control time-end', value: "#{ f.object.time_end.strftime("%I:%M %p") rescue nil }"

Solution

  • in = f.text_field :time_start, required: true, class: 'form-control time-end',

    Please try to add:

    input_html: { value: "#{ f.object.time_start.strftime("%I:%M %p") rescue nil }" }