Вопрос

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

Это было полезно?

Решение

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.

Другие советы

equivalent8, to answer your comment i think you need to add translations for date picker:

  date:
    picker_format: "dd/mm/yyyy"
    formats:
      input: "%d/%m/%Y"
      default: "%d %b %Y"  
      long: "%A %e %B %Y"  

but then i am using simple form with the following

class DatePickerInput < SimpleForm::Inputs::Base
  def input
    input_html_options.merge!(:value => 
      I18n.l(@builder.object.send(attribute_name) || Time.now.to_date, :format => :input), 
      :data => { :date_format => I18n.t('date.picker_format')}
    )
    "#{@builder.text_field(attribute_name, input_html_options )}".html_safe
  end
end

this might help you in your situation

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top