Anchieta Júnior.
Anchieta Júnior.

Reputation: 21

JQuery UI Datepicker shows wrong date when Editing model on Rails

When i'm creating my model @bill i have an attribute called purchase_date. I select the date from datepicker and it works fine, my date in view format is dd-mm-yyyy. But when i call action edit, it brings wrong date format yyyy-mm-dd and my field has a mask besides the datepicker, so it comes like 20/15/1116 and causes error on update. I already tried force a ...

@bill.purchase_date.strftime('%d/%m/%Y')

and parse :value in my field. If i put it in another page element goes right, but in my field don't.

Here is my code.

form.html.erb

<%= f.input :purchase_date, as: :string, :label => false, :required => true, :input_html => { :id => 'datepicker1', :style => 'width: 100px;' } %>

JS

$(function() {
    $( "#datepicker1" ).datepicker({
      showOn: "button",
      buttonImage: "http://s8.postimg.org/kryvquupd/calendar.png",
      buttonImageOnly: true,
      buttonText: "Select date"
    });
  $("#datepicker1").mask("99/99/9999",{placeholder:"dd/mm/aaaa"});
});

Upvotes: 1

Views: 798

Answers (1)

sameera207
sameera207

Reputation: 16629

you can set the date format to datepicker

$( "#datepicker1" ).datepicker({
      showOn: "button",
      buttonImage: "http://s8.postimg.org/kryvquupd/calendar.png",
      buttonImageOnly: true,
      buttonText: "Select date",
      dateFormat: dd-mm-yyyy   
  });

Upvotes: 1

Related Questions