0
votes

After much stress, finally got the datepicker gem to succesfully open a mini-calendar and enter the date in the field. Despite using this jQuery script,

$jQuery ->

$(document).on "focus", "[data-provide~='datepicker']", (e) ->
 - $(this).datepicker
 - format: "yyyy-mm-dd"
 - weekStart: 1
 - autoclose: true;

...the date returned in the field is still in mm/dd/yyyy format. That's fine as long as I want to use a string field in the database (and I may wind up reconstructing the db that way) but I need to do some simple math functions on start_date - end_date, which is why I created the database with date fields instead of string fields in the first place.

So I need to do one of two things--either reformat my db date fields to strings (and use Date.parse() in my math calculations) or find a way to save the date returned to the text field, generated by this:

<div class="field">
    <%= f.label :end_date %><br>
    <%= f.text_field :end_date, 'data-provide' => 'datepicker' %>
  </div>

...as a mysql date field, formatted yyyy-mm-dd. Is there a way to do this in the _form.html.erb document prior to submitting the form?

Or, alternatively, is there a parameter/option in the bootstrap-datepicker-rails plugin that will allow the input field to be a date field instead of a text_field, or to convert the return value to a date instead of a string?

1
On a hunch I used f.date_field instead of f.text_field, and voila, 2 calendar widgets appeared (one generated by bootstrap-datepicker, I assume). The bootstrap widget didn't save the date, but the "new" one did. Does rails include this for free using date_field, or is the gem still generating it? (I erased the javascript commands but not the datepicker.js or .css). If so, why are people struggling with datepicker plugins? New in rails 4, perhaps?notewrangler

1 Answers

0
votes

// here solution
object.date = Date.strptime(params[:date], '%m/%d/%Y').to_date

// change date format according your code