Katie M
Katie M

Reputation: 1131

Rails 3 Object.try Syntax

I have a field that may be blank, so I need to use Object.try, but I'm having trouble with the syntax when adding the date format.

<%= @request.assigned_date.strftime("%m/%d/%y") %>

How would I change the above to include the ".try" ?

Thanks in advance. Katie

Upvotes: 3

Views: 379

Answers (2)

flooooo
flooooo

Reputation: 709

I would try:

<%= @request.assigned_date.try(:strftime, "%m/%d/%y") %>

Tell me if that worked for you.

Upvotes: 2

Robin
Robin

Reputation: 21884

I'd say...

<%= @request.assigned_date.try(:strftime, "%m/%d/%y") %>

Note that it wouldn't work if your field was an empty string. It has to be nil.

Upvotes: 3

Related Questions