2

I am using jquery ui datepicker in my app in a form. when I save the form the date field in shown nil.

I have installed "jquery-ui-rails" gem and given require jquery ui.all in application.js and application.css

My code goes as follows in view

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

In 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. What might be the reason and how to rectify this. Pls help me out of this.

rails.newbie
  • 63
  • 2
  • 11
  • possible duplicate of [ActiveRecord date format](http://stackoverflow.com/questions/2529990/activerecord-date-format) – Stefan Oct 28 '13 at 10:04
  • Man, check that deadline is in permitted params, either in controller if you run rails4, or attr_accessible in model if you have previous rails version – Ivan Shamatov Oct 28 '13 at 10:11

1 Answers1

4

May be its a problem in date format for the db and you entered from 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.

Edit:

In many of the databases the default data format is 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. So the value does not save in db.

Do we need to provide the date format of the date field as dd-mm-yyyy

In jquery datepicker the dateFormat: 'dd-mm-yyyy' sets the date format to the same.

Debadatt
  • 5,935
  • 4
  • 27
  • 40