18

When I submit my form I can see the date being sent to in the post. However, It doesn't save the date. If I do a date check it says it is not in the proper format. Here is my date picker function, it displays fine:

    $j(function(){
    $j("#mile_date").datepicker();
});

the $j is because I am using prototype as well so all jquery calls are using the the noconflict variable.

Here is the post:

 Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"Fj4HW/B4EOan/vcPZLJ75TvWkRH4ZKSFsPLlQLSD0cI=", "mile"=>{"odometer"=>"", "trip"=>"428.2
", "gallons"=>"24.959", "note"=>"", "temperature"=>"", "date"=>"06/22/2011"}, "commit"=>"Create Mile"}

So it sends the date fine but rails doesn't seem to like the format. It inserts a null value into the database. If I submit it with the default datefield with the drop downs it sends this and saves fine:

    Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"Fj4HW/B4EOan/vcPZLJ75TvWkRH4ZKSFsPLlQLSD0cI=", "mile"=>{"odometer"=>"", "trip"=>"428.2
", "gallons"=>"24.959", "mpg"=>"17.156136063144", "note"=>"", "temperature"=>"", "date(1i)"=>"2011", "date(2i)"=>"6", "date(3i)"=>"22"}, "c
ommit"=>"Create Mile"}

In the insert statement it inserts the date as:'2011-06-22'

Does Rails expect the date in 3 variables to construct the date format correctly? How can I get the datepicker to send the correct date format?

Thank you in advance,

Xaxum
  • 3,545
  • 9
  • 46
  • 66

6 Answers6

36

I ran into the same issue. One solution is to simply change the format that the datepicker uses:

// in your javascript...
$j(function(){
  $j("#mile_date").datepicker({
    dateFormat: "yy-mm-dd"
  });
});

Rails seems to be able to handle the yy-mm-dd format - I'm using that and am having no issues saving the date to the database. The only issue here is that some might find the yy-mm-dd format a little less good looking than mm/dd/yyyy...

BaronVonBraun
  • 4,285
  • 23
  • 23
  • 2
    Posted a solution where user sees one way and you submit proper format with hidden_field. Again thanks for pointing to original problem since I saw elsewhere that rails would convert the date. – Xaxum Aug 25 '11 at 15:24
  • just for people running in the same problem, the name of the parameter is `format` and not `dateFormat` anymore – Vincent Rolea Jun 14 '17 at 08:57
20

As noted by @BaronVonBraun above rails doesn't seem to handle that format. Changing it as he suggested worked. However, for those wanting a different format than yy-mm-dd you can use the following. The user sees the format you want while rails gets the format it needs.

$j(function(){
    $j("#show_date").datepicker({altField: '#mile_date', altFormat: 'yy-mm-dd'});
});

The show_date is the id of the field they see and the mile_date is a hidden field with the date rails needs.

Here is the documentation.

Xaxum
  • 3,545
  • 9
  • 46
  • 66
1

If anyone is using the jquery_datepicker gem , you'll want to use something similar to the following code in your rails view.

<%= form.hidden_field(:ship_date, :id => "ship_date") %>   
<%= datepicker_input(:show_date, item.id, :size => 10, altField: "#ship_date", altFormat: 'yy-mm-dd', :value => item.ship_date.strftime("%m/%d/%Y"))%>

You can also use form.datepicker_input to attach the the date picker directly to the form, but in my use case, I wanted the date picker to reflect the localized date, which Rails would not accept. So I added a hidden form element and set the alternate field to it, works perfectly!

ARun32
  • 355
  • 1
  • 8
0

Kinda of hacky but made a helper the set things straight when I know I am giving the server dates in the mm-dd-yy format

def convert_to_y_m_d(date)
  new_date = date.split("-")[2] + "-" + date.split("-")[0] + "-" + date.split("-")[1]
  new_date
end
ALFmachine
  • 639
  • 7
  • 12
0

Or try the delocalize gem: https://github.com/clemens/delocalize

bonkydog
  • 2,012
  • 20
  • 11
0
j('.jquery-calendar').datepicker().each(function(){
  // convert dates from db to US format
  var $input = j(this)
  var found = $input.val().match(/^(\d{4})-(\d{2})-(\d{2})$/);
  if(found){
    $input.val(found[2]+'/'+found[3]+'/'+found[1]);
  }
});
grosser
  • 14,707
  • 7
  • 57
  • 61