ruby-on-rails

rails i18n - How to handle case of a nil date being passed ie l(nil)


I am working with a lot of legacy data and occasionally a datetime field is nil/null. This breaks the localization. Is there a recommended way of fixing this aside from doing this:

dt = nil
l(dt) unless dt.nil?

Solution

  • Unfortunately, there is no built-in solution. See post.

    You can define your own helper that supplies the "nil" human-readable value. Eg:

    def ldate(dt)
      dt ? l(dt) : t("[???]")
    end