简体   繁体   English

使用jquery datepicker rails日期未保存在数据库中

[英]date not saved in database by using jquery datepicker rails

I am using jquery ui datepicker in my app in a form. 我在应用程序中以某种形式使用jquery ui datepicker。 when I save the form the date field in shown nil. 当我保存表单时,日期字段显示为nil。

I have installed "jquery-ui-rails" gem and given require jquery ui.all in application.js and application.css 我已经安装了“ jquery-ui-rails” gem,并在application.js和application.css中给出了需要jQuery ui.all的信息

My code goes as follows in view 我的代码如下

 <div>
  <%= f.label :deadline %>
  <%= f.text_field :deadline, :id => "datepicker" %>
</div> 

In application.js 在application.js中

  $(function() {
$( "#datepicker" ).datepicker({
    minDate: 0
 });
});

On submitting the form 提交表格时

    Parameters: {"utf8"=>"✓","authenticity_token"=>"aoiDOLAGZHKui85hum7ozJ31QNZopOvmImyOuDyXCw4=", "task"=>{"task"=>"task1", "assigned_to_id"=>"2", "deadline"=>"10/30/2013", "project"=>"", "workspace_id"=>"4"}, "commit"=>"Create", "id"=>"4"}

Insert Into: 插入:

   INSERT INTO "tasks" ("assigned_to_id", "created_at", "project", "task", "updated_at", "user_id", "workspace_id") VALUES (?, ?, ?, ?, ?, ?, ?)  [["assigned_to_id", 2], ["created_at", Mon, 28 Oct 2013 09:51:22 UTC +00:00], ["project", ""], ["task", "task1"], ["updated_at", Mon, 28 Oct 2013 09:51:22 UTC +00:00], ["user_id", 1], ["workspace_id", 4]]

Here deadline field is not present. 截止日期字段不存在。 It is shown as nil.I have checked the schema for spell check too... everything is correct. 它显示为nil。我也检查了模式以进行拼写检查...一切正确。 What might be the reason and how to rectify this. 可能是什么原因以及如何纠正此问题。 Pls help me out of this. 请帮助我。

May be its a problem in date format for the db and you entered from datepicker db的日期格式可能是一个问题,而您是从datepicker输入的

Change the date format in datepicker. 更改日期选择器中的日期格式。 like 喜欢

$(function() {
$( "#datepicker" ).datepicker({
    minDate: 0
    dateFormat: 'dd-mm-YYYY'
 });
});

Or you can change the date format in controller. 或者,您可以在控制器中更改日期格式。

The default format of date is date-month-year. 日期的默认格式为date-month-year。

Edit: 编辑:

In many of the databases the default data format is dd-mm-yyyy 在许多数据库中,默认数据格式为dd-mm-yyyy

So we need to provide the date format from our date picker with the same data format. 因此,我们需要为日期选择器提供的日期格式具有相同的数据格式。

But in jquery ui datepicker the dafault data format is dd/mm/yyyy which is not an acceptable format with db. 但是在jquery ui datepicker中,dafault数据格式为dd/mm/yyyy ,这是db不能接受的格式。 So the value does not save in db. 因此,该值不会保存在db中。

Do we need to provide the date format of the date field as dd-mm-yyyy 我们是否需要将日期字段的日期格式提供为dd-mm-yyyy

In jquery datepicker the dateFormat: 'dd-mm-yyyy' sets the date format to the same. 在jQuery datepicker中, dateFormat:'dd-mm-yyyy'将日期格式设置为相同的格式。

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

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