Rails date format in a text_field

后端 未结 14 2195
傲寒
傲寒 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:42

    So I did something like this in the model.

    def start_date
      super.strftime "%Y/%m/%d %H:%M"
    end
    

    Say you have a column called start_date in model, and want to display another format in the form, just overwrite its value in model.

    And nothing needs to be changed in the form.

    <%= f.text_field :start_date, class: 'form-control' %>
    

提交回复
热议问题