In a Rails view, one can use try to output only if there is a value in the database, e.g
@model.try(:date)
And one can chain trys if, for example, the output is needed as a string
@model.try(:date).try(:to_s)
But what if I need to call a scoped format? I’ve tried
@model.try(:date).try(:to_s(:long))
@model.try(:date).try(:to_s).try(:long)
What is the correct syntax for this? And what is a good reference for more explanation?
Thanks
From the fine manual:
So I think you want:
This one won’t work:
because you’re trying to access the
:to_ssymbol as a method (:to_s(:long)). This one won’t work:because you’re trying to call the
longmethod on whatto_sreturns and you probably don’t have aString#longmethod defined.