Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 596
Katie M Avatar asked Feb 28 '26 13:02

Katie M


1 Answers

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.

like image 90
Robin Avatar answered Mar 03 '26 03:03

Robin