Fixing an error with Datepicker and using Rack Protection EscapedParams with Jump Start Sinatra project

I’ve been going through the Jump Start Sinatra book and nearing the end.

I’m checking out Rack::Protection::EscapedParams but when I try to update with jQuery’s Datepicker I get an ArgumentError - invalid date: 500 Internal Server Error. Any ideas or why this error occurs?

This is what my date looks like:

class Song
  ... 
    def released_on=date
      super Date.strptime(date, '%m/%d/%Y')
    end
end

Thanks!

I believe I’ve found the answer to my problem. I think it has to do with the original format with the forward slash and how Rack::Protection::EscapedPams handles those. I fixed my problem by explicitly setting the date format coming from the Datepicker object. The default Datepicker format is “02/08/2015”

Adding dateFormat solved the problem:

# application.coffee (excerpt)
$('#released_on').datepicker( dateFormat: "yy-mm-dd", changeYear: true, yearRange: '1940:2000' )

and

# song.rb (excerpt)
def released_on=date
  super Date.strptime(date)
end

It seems like if I wanted to use dateFormat: "mm-dd-yy" in Datepicker() then super Date.strptime(date, '%m-%d-%Y') works too.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.