Formatting a date input using simple_form

前端 未结 2 1902
时光取名叫无心
时光取名叫无心 2021-01-11 18:51

I have a date type field in my database.

Here\'s the view code I\'m using:

# Using \'as: :string\' to avoid those three dropdowns rails          


        
相关标签:
2条回答
  • 2021-01-11 19:09

    Here's what you need :

    f.input :my_date,
            :as => :string, 
            :input_html => { :value => localize(f.object.my_date) }
    

    PS : This was the first link in google search :)

    0 讨论(0)
  • 2021-01-11 19:32

    I created this class which makes it easy to do this:

    f.input :my_date, :as => :date_picker
    

    Add this class to your lib/ folder and be sure to include it (or configure it to autoload):

    class DatePickerInput < SimpleForm::Inputs::StringInput
      def input
        value = @builder.object.send(attribute_name)
        input_html_options[:value] = case value
                                       when Date, Time, DateTime
                                         format = options[:format] || :medium
                                         value.to_s(format)
                                       else
                                         value.to_s
                                     end
    
        input_html_options[:class] ||= []
        input_html_options[:class] << "date_picker_input"
        @builder.text_field(attribute_name, input_html_options)
      end
    end
    
    0 讨论(0)
提交回复
热议问题