Rails date format in a text_field

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

    For people interested to format in a specific location and not all over the project, several answers advised to check if null and I want to add that using try with strftime will help to make it shorter.

                <%= f.fields_for :assets_depreciations do |asset| %>
                  
                    
                      <%= asset.text_field :depreciation_date, value: asset.object.depreciation_date.try(:strftime, "%d-%m-%Y") %>
                    
                  
                <% end %>
    

提交回复
热议问题