带有 Rails 4 的 jQuery 日期选择器 [英] jQuery Datepicker with Rails 4

查看:13
本文介绍了带有 Rails 4 的 jQuery 日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery Datepicker gem,当我编辑具有日期的记录时,它的行为异常.

当我创建新记录时,日期以 mm/dd/yy 格式输入.当我去编辑记录时,日期以 yyyy-dd-mm 格式填充到日期字段中,我不知道为什么.也许这与 Rails 本身有关?

表中的日期列设置为日期,我认为这很好.在控制器中显示日期或其他内容时,我是否需要在视图中执行某些操作?

这是我的 application.js 文件中的内容:

$(document).on('page:change', function () {$('#cust_dob').datepicker({changeMonth: 真,更改年份:真实,dateFormat: "mm/dd/yy",年范围:1900:+0"});});

任何帮助将不胜感激.

解决方案

好的,我能够让它工作.有两个问题.首先是由于格式不同,日期并不总是被保存到数据库表中.所以,我使用了一个隐藏字段,并使用了数据选择器的 altFormat 和 altField 选项.第二个问题是在编辑记录时格式化当前日期.这是按照 DevDue 建议使用 strftime 实现的.所以,对于那些对此感兴趣的人,我认为是这样.并不是说这是最好的方法,因为我仍然在学习 RoR,但它确实有效 :)

所以,我对日期的部分看法:

<% dob_display = :dob %><%= f.text_field dob_display, placeholder: "mm/dd/yyyy", required: true, id: "cust_dob", value: @customer.dob.try(:strftime, "%m/%d/%Y")%><%= f.hidden_​​field :dob, id: "cust_dobalt" %>

我花了一段时间才意识到,除非 hidden_​​field 出现在可见字段之后,否则它根本不起作用!

我用于此日期选择器的 application.js 文件如下所示.注意 altField 和 altFormat:

$(document).on('page:change', function () {$('#cust_dob').datepicker({changeMonth: 真,更改年份:真实,dateFormat: "mm/dd/yy",年范围:1900:+0",altField: "#cust_dobalt",altFormat: "yy-mm-dd",持续时间:慢"});});

I'm using jQuery Datepicker gem and when I'm editing a record that has a date it it behaves unexpectedly.

When I create a new record the date is entered with the mm/dd/yy format. When I go to edit the record the date is being populated in the date field in the format yyyy-dd-mm and I have no idea why. Perhaps this is something to do with Rails itself?

The Date column in the table is set to Date, which I would imagine is fine. Is there something that I need to do in my view when displaying the date or something in the controller?

This is what is in my application.js file:

$(document).on('page:change', function () {
        $('#cust_dob').datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: "mm/dd/yy",
            yearRange:  "1900:+0"
    });
});

Any help would be much appreciated.

解决方案

Ok, I was able to get this working. There were two issues. The first is that the date wasn't always getting saved into the database table because of a difference of format. So, I used a hidden field and used the altFormat and altField options of the datapicker. The second issue was formatting a current date when editing a record. This was achieved using strftime as DevDue suggested. So, for those that are interested here is what I have in my view. Not saying this is the best way since I really am still learning RoR, but it works :)

So, my partial view with the date:

<div class="span2 input">
  <% dob_display = :dob %>
  <%= f.text_field dob_display,  placeholder: "mm/dd/yyyy", required: true, id: "cust_dob", value: @customer.dob.try(:strftime, "%m/%d/%Y") %>
  <%= f.hidden_field :dob, id: "cust_dobalt" %>
</div>

It took me a while to realise that unless the hidden_field comes after the visble field it just doesn't work!

My application.js file for this datepicker looks as follows. Notice the altField and altFormat:

$(document).on('page:change', function () {
    $('#cust_dob').datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: "mm/dd/yy",
        yearRange:  "1900:+0",
        altField:   "#cust_dobalt",
        altFormat:  "yy-mm-dd",
        duration:   "slow"
    });
});

这篇关于带有 Rails 4 的 jQuery 日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆