2

My question is similar to this SO problem, but it does not answer my question:

Rails 3 default datetime format without UTC

I understand that I can add

Time::DATE_FORMATS[:default] = "%Y/%m/%d"

to my environment.rb and it will change my default time format accordingly.

So when I do Time.now.to_s I get the correct format.

However, my question is, how come this does not work with a form_builder. For example:

<%= f.text_field :date %>

will return a full UTC timestamp: 2013-01-25 07:45:21

I am aware that I can do this

<%= f.text_field :date, :value => @post.date.to_s %>

And it will give me the correct format.

But this solution seems hacky to me. Is this really the only way to do it?

Community
  • 1
  • 1
  • 1
    How would you like your input to look like? The Rails [date_select](http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select) helper is really flexible to customize and might fit your needs better than a text-field. – Thomas Klemm Jan 25 '13 at 07:57

2 Answers2

0

I am not 100% sure. But the answer is most likely no. The rails formhelper just gives you whatever value that is in your database.

Here is where the value is retrieved for your reference:

http://api.rubyonrails.org/classes/ActionView/Helpers/InstanceTag.html#method-i-to_input_field_tag

There is no special processing of it, such as localize or to_s, which is where formatting is applied.

vinhboy
  • 8,542
  • 7
  • 34
  • 44
-1

If the date looks like a timestamp (2013-01-25 07:45:21 UTC),

You could do something like the following to views you localize timestamps with:

<%= localize(@post.date, :format => :long) %> 

According to your problem, you could try using date_select rather than text_field.

<%= f.date_select :date, :order => [:day, :month, :year] %>
Sri
  • 2,233
  • 4
  • 31
  • 55
  • I am not sure this is the answer he is looking for. He is specifically asking how to get the correct format using the form builder. – vinhboy Jan 25 '13 at 16:54