# How to set the current date to a text\_field

**URL:** <https://www.ruby-forum.com/t/how-to-set-the-current-date-to-a-text-field/137484>\
**Category:** Rails\
**Created:** [May 13, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/how-to-set-the-current-date-to-a-text-field/137484 "2008-05-13T16:30:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dingirfecho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dingirfecho](https://www.ruby-forum.com/u/dingirfecho)\
**Post date:** [May 13, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/how-to-set-the-current-date-to-a-text-field/137484/1 "2008-05-13T16:30:52Z")

</div>

Hi!  
I’m new to Rails, and I have an app which uses text\_fields. How can I  
set the default value to the current date? I’m using Rails 2.

thanks!

---

<div class="post-metadata">

**Author:** ![dingirfecho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dingirfecho](https://www.ruby-forum.com/u/dingirfecho)\
**Post date:** [May 13, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/how-to-set-the-current-date-to-a-text-field/137484/2 "2008-05-13T16:37:11Z")

</div>

Federico Andino wrote:

> Hi!  
> I’m new to Rails, and I have an app which uses text\_fields. How can I  
> set the default value to the current date? I’m using Rails 2.
> 
> thanks!

Someone told me! I’ve set it into the controller…thanks!

---

<div class="post-metadata">

**Author:** ![dingirfecho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dingirfecho](https://www.ruby-forum.com/u/dingirfecho)\
**Post date:** [May 14, 2008, 1:26pm UTC](https://www.ruby-forum.com/t/how-to-set-the-current-date-to-a-text-field/137484/3 "2008-05-14T13:26:56Z")

</div>

You can set this via the clients browser  
\<%= Time.now %\>  
or via a call to the controller (Dry it up)  
IE

def index  
@current\_time = Time.now  
end

\<%= @current\_time %\>

in your textfield use the @ current\_time method

If you are using 2.0 + their is date\_field that allows the setting on  
the date/time

IE:  
\<% form\_for(@your\_form\_object) do |f| %\>

**Due**  
 \<%= f.datetime\_select :due %\>

\<% end %\>

PS  
using the above form defaults to the current date – be careful of how  
this goes into the db

If this info is coming from the db you may want to edit the update  
method in the controller so when a item/product is edited/updated  
Dooooooo disregard that and read on…  
If you have used the default rails migration scripts you should have  
two time fields automatically included in your models [tables]  
(created\_at and updated\_at)

as soon as you update/edit the info in this record the updated\_at  
should reflect the time the date was edited you could use that ??

Sorry about the rant  
Hope this helps

On May 13, 10:37 am, Federico Andino \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![dingirfecho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dingirfecho](https://www.ruby-forum.com/u/dingirfecho)\
**Post date:** [May 14, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/how-to-set-the-current-date-to-a-text-field/137484/4 "2008-05-14T16:12:47Z")

</div>

edited you could use that ??

> Sorry about the rant  
> Hope this helps
> 
> On May 13, 10:37 am, Federico Andino \<rails-mailing-l…@andreas-

Thanks! I’ll refine the date with this!
