Search code examples
ruby-on-railsruby-on-rails-3meta-searchransack

how to tell ransack gem to recognize datepicker US format


How to tell ransack gem to translate US format mm/dd/yyyy (deffault from bootstrap datepicker) ?

project I'm working on will have several lacales files (UK, US), so UK is simple (just change the datepicker format option) but US format is problem

Example:

= search_form_for @post do |f|
  f.input :created_at_gt,7

enter image description here

params:

{"utf8"=>"✓", "q"=>{"created_at_gt"=>"07/04/2012",....}

sql:

... `posts`.`created_at` > '2012-04-07 00:00:00') LIMIT 25 OFFSET 0

(should be created_at > '2012-07-04 00:00:00' )

Note: I know I could handle that on controller level, but I'm looking for solution where Ransack will automatically pick up locales configuration and parse that way


Solution

  • My colleague told me to always send yyyy-mm-dd format to the server and just display the date on client side in whatever format users wish.

    Now,how to do that? Add extra hidden field and with JS set that field to DB format when user chooses date with datepicker. More in this gist.