Format DateTime in Text Box in Rails

前端 未结 7 1841
生来不讨喜
生来不讨喜 2021-01-31 18:59

Is there an easy way to format the display of a DateTime value in a Rails view?

For example, if I\'m doing something like:

<%= text_field         


        
7条回答
  •  长情又很酷
    2021-01-31 19:38

    I can think of three ways:

    1) Creating a new method on the model that would format your date however you need it to be

    2) Creating a helper method that would do the same

    3) Have you tried doing:

    <%= text_field 
       :my_object,
       :start_date,
       :value => @my_object.try(:start_date).try(:strftime,'%m/%d/%Y') %>
    

提交回复
热议问题