Cannot translate date formats from rails form to mysql db

Posted by Steve on Stack Overflow See other posts from Stack Overflow or by Steve
Published on 2013-10-30T22:13:01Z Indexed on 2013/10/31 3:54 UTC
Read the original article Hit count: 97

I have a simple search form in rails 3 that has two date fields. I'm having a problem getting these dates into my mysql db.

I've tried using the american_date gem, specifying date formats in my initializers, in the config/locales/en.yml file, and directly on the date on the date fields themselves. Currently, I'm setting the rails-approved date format in the view -

<%= f.text_field :depart_date, :value=> Date.today.strftime('%Y-%m-%d') %>

The dateformat in my DB is also YYYY-mm-dd, so things should be going smoothly.

The console tells me that the two date fields are both class = "Date"

I think I've found the disconnect. From the logs -

Started POST "/searches" for 127.0.0.1 at 2013-10-30 17:43:26 -0400
Processing by SearchesController#create as HTML
Parameters: {"utf8"=>"v","search"=>{"depart_date"=>"2013-10-30", 
"return_date"=>"2013-11-09"}
?[1m?[35m (0.0ms)?[0m  BEGIN
?[1m?[36mSQL (0.0ms)?[0m  ?[1mINSERT INTO `searches` (`depart_date`,`return_date`)
VALUES ('2013-30-10','2013-09-11')?[0m
?[1m?[35m (2.0ms)?[0m  COMMIT

Note that the month and day values are switched in the insert statement. How can I prevent this from happenening?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about ruby-on-rails