jQuery Datepicker with Rails 4 [英] jQuery Datepicker with Rails 4
问题描述
当我创建一个新的记录时,输入的日期是mm / dd / yy格式。当我去编辑记录时,日期字段中填充日期字段,格式为yyyy-dd-mm,我不知道为什么。也许这与Rails本身有关吗?
表中的Date列设置为Date,我想象的很好。在控制器中显示日期或某些东西时,有什么需要做的吗?
这是我的application.js文件中的什么:
$(document).on('page:change',function(){
$('#cust_dob')。 datepicker({
changeMonth:true,
changeYear:true,
dateFormat:mm / dd / yy,
yearRange:1900:+0
} );
});
任何帮助将不胜感激。
好的,我能够得到这个工作。有两个问题。
首先,由于格式不同,日期并不总是被保存到数据库表中。所以,我使用了一个隐藏的字段,并使用datapicker的altFormat和altField选项。第二个问题是在编辑记录时格式化当前日期。这是使用strftime作为DevDue建议实现的。所以,对于那些对我有兴趣的是我所看到的。不,这是最好的方式,因为我真的还在学习RoR,但它的作品:)
所以,我的部分视图与日期:
< div class =span2 input>
<%dob_display =:dob%>
<%= f.text_field dob_display,占位符:mm / dd / yyyy,必需:true,id:cust_dob,value:@ customer.dob.try(:strftime,%m / d /%Y)%>
<%= f.hidden_field:dob,id:cust_dobalt%>
< / div>
我花了一段时间才意识到,除非hidden_field来到了visfield字段之后,工作!
我的这个datepicker的application.js文件如下所示。注意altField和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
});
});
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"
});
});
这篇关于jQuery Datepicker with Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!