2008 February

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

| development (web) | No Comments

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 = Time.now.strftime(“%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: http://au2.php.net/strftime for details on formatting dates using strftime