Leo
Leo

Reputation: 2103

Saving date from datepicker

In my form i'm using datepicker plugin. it works fine, and my form code looks like this:

= simple_form_for @person do |f|
    = f.input :born, input_html: {class: "datepicker"}, as: :string
    = f.input :died, input_html: {class: "datepicker"}, as: :string
    = f.submit

However, params I get are in a form of string, like "2015/02/04" and they are not saved in the database. with classic input params look competely different. How can I make these params save into the database?

Upvotes: 0

Views: 787

Answers (2)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Your date string looks like 2015/02/04 which is default format of datepicker plugin, but to save it to the database you need to change it to 2015-02-04 format. So you need to do something like this in the action

params[:born] = params[:born].gsub("/", "-")

or

you need to pass an option when you are initializing the datepicker plugin, like give below

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

Hope this helps!

Upvotes: 1

Vrushali Pawar
Vrushali Pawar

Reputation: 3803

2.1.0 :049 > d = "2015/02/04"
 => "2015/02/04" 
2.1.0 :050 > Date.parse(d, "%Y/%M/%d")
 => Wed, 04 Feb 2015 

Using this date will be saved

Upvotes: 0

Related Questions