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
This can be handled DRYly at the model layer using the Rails-provided
method variant. Rails already uses this method for doing just this type of formatting, so we're just overwriting the default. For example:
class MyModel
def start_date_before_type_cast
return unless self[:start_date]
self[:start_date].strftime('%m/%d/%Y')
end
end
This way, anywhere you have a text field for the MyModel#start_date field you get formatting for "free".