rails if object.nil? then magic '' in views?

前端 未结 8 1373
清歌不尽
清歌不尽 2020-12-23 16:46

This is one of those things, that maybe so simple I\'ll never find it because everyone else already knows it.

I\'ve got objects I have to check for nil in my views s

8条回答
  •  星月不相逢
    2020-12-23 17:36

    The Ruby community has put an incredible amount of attention to automating this idiom. These are the solutions I know of:

    • try in Ruby on Rails
    • Another try
    • andand
    • A safer andand
    • Kernel::ergo
    • send-with-default
    • maybe
    • _?
    • if-not-nil
    • turtles!
    • method_ in Groovy style
    • do-or-do-not

    The most well-known is probably the try method in Rails. However, it has received some criticism.

    In any case, I think Ben's solution is perfectly sufficient.

提交回复
热议问题