0

I'm using bootstrap-datepicker-rails (v. 1.3.0.2) in my rails 4.0.9 project, and seeing some strange behavior with it in development and production, as the title explains. I've already had a look at this question, but it seems my issue is not the same, and more bizarre, than the OP's.

Like him, I'm getting an argument out of range error when I submit my form (whether it be for a create action or an update action), but only for some dates, and not for others.

For example, this date is out of range (copied from my development log - a snippet from the parameters sent to the controller):

"date_received"=>"08/28/2014"

Yet this date is OK:

"date_received"=>"08/07/2014"

I think the fact that some date selections save correctly rules out a formatting issue. I'm stumped as to how this could be happening. Any date before 08/13/2014 works, and any date after that returns the out of range error.

Here's the ugly details:

application.js:

//= require jquery
//= require jquery.turbolinks
//= require jquery.tablesorter
//= require jquery_ujs
//= require bootstrap
//= require bootstrap-datepicker/core
//= require jquery-ui
//= require turbolinks

application.css:

*= require jquery-ui/theme
*= require bootstrap-datepicker
*= require font-awesome
*= require custom
*= require theme.blue
*= require_self

jobs.js.coffee:

$(document).ready ->

  $ ->
    $(".tablesorter").tablesorter( {sortList: [[2,0]]} )
    $('.datepicker').datepicker()

    return

job.rb:

(validation for the field in question):

validates :date_received, presence: true

jobs_controller.rb:

(line that the error occurs on):

if @job.update_attributes(job_params)

job_params definition:

def job_params
  params.require(:job).permit(:number, :name, :display_name, :date_received, :market, 
  :job_type, :pose_selection, :pose_selection_label, :pose_selection_deadline, 
  :pk_id, :pk, :flyout_id, :flyouts, :code, :tax_rate, :shipping_handling, :mail_home, 
  :mail_home_amount, :line, :notes, :entered, :entered_by, :verified, :verified_by, 
  :printed, :printed_by, :assembled_by, :shipped, :active, :discount_amount, :data)
end

Where do I go from here to troubleshoot this?

Community
  • 1
  • 1
digijim
  • 676
  • 2
  • 9
  • 20
  • Date format issue? Is it assuming dd/mm/yy ? have you tried 20/08/14 and is that considered in range? – SteveTurczyn Aug 29 '14 at 21:11
  • I just now played around with typing the date in manually rather than picking via datepicker. If I enter the date "20140828", it IS in range. So is "20140807". Still don't understand how it's erroring via datepicker. – digijim Aug 29 '14 at 21:13
  • It gets weirder. Dates starting in September, 2014 selected via datepicker work OK, too. So it's just the dates from 08/13/2014 to 08/31/2014 that fail. All other dates I've tried have worked. I need a drink. – digijim Aug 29 '14 at 21:40
  • Did you try Ssptember 15 via date picker? – SteveTurczyn Aug 29 '14 at 21:41
  • I mean September 15 2014 just to be clear... – SteveTurczyn Aug 29 '14 at 21:42
  • September 15 fails. Same with July 30, July 31, but not July 1. – digijim Aug 29 '14 at 21:43
  • Can you make it work with a Date instead of a String? Hint: there are only 12 months in a year. – Andrew Morton Aug 29 '14 at 21:43
  • Can you elaborate, @AndrewMorton? Not sure how to accomplish what you're asking. – digijim Aug 29 '14 at 21:45
  • 1
    It's clear it's reading the string as d/m/y format (British format). Any "month" (middle) value greater than 12 fails. – SteveTurczyn Aug 29 '14 at 21:46
  • 1
    @digijim If you use a Date then the date is unambiguous, regardless of locale. E.g. 1/1/2014 is the 1st of January, 2014 in most of the world, but January 1st, 2014 in a few locales (notably the USA). A Date of (2014, 1, 1) is unambiguous because it is specified as (year, month, day). I don't know about the internals of bootstrap-datepicker-rails, but it really should allow for using a Date rather than a String. – Andrew Morton Aug 29 '14 at 21:52
  • Thanks for the replies. I'm gonna pick this up again on Tuesday and see what I can do to get my form to submit a Date rather than a String. Cheers! – digijim Aug 29 '14 at 22:17

1 Answers1

1

The default format for a date that bootstrap-datepicker-rails returns is "mm-dd-yyyy". Submitting dates formatted this way either saves the date incorrectly, or results in "out of range" errors, because Rails is misinterpreting the day as the month. h/t to @SteveTurczyn for opening my eyes to this fact.

@AndrewMorton suggested making bootstrap-datepicker-rails submit a Date rather than a String, but there's no such mechanism built into that gem, AFAIK.

So I needed to change the format of the String being returned by datepicker. After some console testing, I found that a format of "dd-mm-yyyy" would successfully save (among other formats, of course).

Changing this line (from jobs.js.coffee):

$('.datepicker').datepicker()

... to this:

$('.datepicker').datepicker( { dateFormat: 'dd-mm-yy' } )

forces the date returned to the form to be in a format of "dd-mm-yyyy".

The only downside to this fix is that after selecting a date via the datepicker, the date displayed on the form is not formatted for the U.S. audience (e.g. "23-09-2014" instead of "09-23-2014"). But in all other views I'm able to display the U.S. formatting via a strftime conversion. It's a sacrifice I'm willing to make for simplicity's sake. At least until I don't have bigger fish to fry.

UPDATE: This is really a terrible 'solution.'

After too much searching for anybody's good, I came across this post which does a much better job of using datepicker with U.S. date formatting. It has it's drawbacks (see my comment on the post), but will work for my purposes.

Community
  • 1
  • 1
digijim
  • 676
  • 2
  • 9
  • 20