Rails date format in a text_field

后端 未结 14 2168
傲寒
傲寒 2020-12-13 06:05

I have a rails form that displays a date in a text_field:

<%= form.text_field :check_in_date  %>

The date is rendered as yyyy-m

14条回答
  •  攒了一身酷
    2020-12-13 06:30

    Expanding Kamil's answer a bit: add the following method to application_helper.rb:

    def date_mdY(date)
      if date.nil?
        ""
      else
        date.strftime("%m-%d-%Y")
      end
    end
    

    Then you can modify Kamil's answer slightly:

    <%= f.text_field :some_date, :value => date_mdY(@model_instance.some_date) %>
    

    Then you can use this helper method in any other view.

    I'm using the jQuery datepicker and the date needed to be in a particular format in order for the default date to be set correctly. Thanks for the answer Kamil!

提交回复
热议问题