Ruby: Displaying current date/time as default in text field

This is pretty basic stuff, but I couldn’t easily find the details on how to do it, so here it is for anyone else who is looking for it.

Scenario: I wanted to put the current date and time as the default text for the date field in a form.

The code:

In the form I used:

<p> Date: <br /> <%= f.text_field :date, :value => @current_time %> </p>

And in the controller for the view put:

@current_time =“%Y-%m-%d %H:%M”)


You can change the formatting of the date/time displayed, but I am using this for now as this way it puts it straight into MySQL without me having to rework it.

Additional info:

Refer to: for details on formatting dates using strftime

Leave a comment

Your email address will not be published. Required fields are marked.