views:

1426

answers:

6

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 :my_object, :start_date %>

and only want to display the Date part of the :start_date (i.e. I want to hide the time part), is there a way to format the :start_date string inside of the view such that it will work for creating new my_object items and updating new my_object items?

Just a clarification:

Doing

<%= text_field 
       :my_object,
       :start_date,
       :value => @my_object.start_date.strftime('%m/%d/%Y') %>

works beautifully when the object already exists (i.e. on item updates), however, when creating a new item, since start_date will initially be nil, the view will throw an error

while evaluating nil.strftime
+1  A: 

If your form is tied to an object, (something like <% form_for(:object) do |f| %>) you should use <%= f.text_field :start_date, :value => f.object.start_date.to_s(:date_format) %>

then you can register your :date_format in config/environment.rb

Not sure why you want to do this, but i hope it's for presentation purposes only.

Eimantas
It's mostly to prevent someone from thinking they need to or should input a time. For most users "2019-09-10 17:00:18" just isn't friendly. I'd much rather just be displaying "09/10/2009" since the time part is completely unnecessary for what I'm doing.
jerhinesmith
then you should use select_date helper instead of plain input
Eimantas
Agree, if it's for selecting a date, a Calendar or select_date helper are better approaches
Yaraher
I am using a calendar control, which works great when selecting a date -- you put the cursor in the text box, select the date from the calendar, and see your selection get written to the textbox. However, when going back to the page, the textbox is going to display the datetime from the DB, meaning that it will again have the time in there -- which is confusing. I'd personally rather not use select_date since the usability doesn't seem near as high as the calendar control I'm already using. Does that make sense?
jerhinesmith
+1  A: 

http://railscasts.com/episodes/32-time-in-text-field

philipth
Uck, I was afraid of that. I was really hoping to avoid having to change the model for what is essentially a presentation-only thing.
jerhinesmith
how about sending in a default value when creating .. i think that should take care of the error
philipth
+2  A: 

I can think of three ways:

1) Creating a new method on the model that would format your date however you need it to be

2) Creating a helper method that would do the same

3) Have you tried doing:

<%= text_field 
   :my_object,
   :start_date,
   :value => @my_object.try(:start_date).try(:strftime,'%m/%d/%Y') %>
Yaraher
Number 3 worked beautifully for me. Thanks.
brad
A: 

if you don't want to enter the date in a textfield, but in a less error prone select field, you could use the ActionView::Helpers::DateHelper, select_date(). see Accessing date() variables send from the date helper in rails here on SO on how to process the return values.

ax
A: 

Some great suggestions here, however in those cases where you really do want your text field date formatted a specific way (say you're integrating with a javascript date picker), and you're dealing with nils, I think simply making a minor tweak to your existing method would do the trick:

:value => (@object.start_date.blank? ? '' : @object.start_date.strftime('%m/%d/%Y')
Dave Rapin
A: 

Great thanks to Dave Rapin!!! Your decision is exactly for me, fantastic!!!

Oleg Guseynov