简体   繁体   English

在编辑表单上的Rails 4上隐藏jquery.timepicker的空日期值

[英]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". 我正在使用John Thornton的JQuery Timepicker ,它可以很好地用作“新”表单上的下拉列表(意味着该下拉列表将使用值“ 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". 但是,当我呈现EDIT表单并且time_start或time_end列具有值时,输入字段将添加日期,并显示为“ 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. 我可以单击下拉菜单中的“ 09:00 AM”以更改输入内容,但我希望现有的time_start或time_end值可以在输入字段中呈现而没有日期。 It's very strange. 真奇怪

I am using rails 4.2 and simple_form and my db field is a time field, not a datetime. 我正在使用rails 4.2和simple_form,而我的数据库字段是时间字段,而不是日期时间。

Here is my rails snippet: 这是我的rails片段:

project/edit.html.slim 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: 这是我在同一页面上调用的jQuery:

  $('#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. 解决方案:感谢以下答案,我有了解决方案...但是我不得不将其翻译为“ simple_form”。 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 }"

in = f.text_field :time_start, required: true, class: 'form-control time-end' , 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 }" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM