rails 如果 object.nil?然后魔术''在意见? [英] rails if object.nil? then magic '' in views?
本文介绍了rails 如果 object.nil?然后魔术''在意见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是其中之一,也许很简单,我永远找不到它,因为其他人都已经知道了.
This is one of those things, that maybe so simple I'll never find it because everyone else already knows it.
我有一些对象必须在视图中检查 nil,所以我不会取消引用 nil:
I've got objects I have to check for nil in my views so I don't dereference a nil:
<%= if tax_payment.user; tax_payment.user.name; end %>
或者我可以做这个变体:
Or I could do this variant:
<%= tax_payment.user ? tax_payment.user.name : '' %>
所以这没问题……对于大多数语言.但我觉得一定有一些闪亮的红宝石或横梁,如果这是我能做的最好的,我仍然想念.
So this is ok ... for most languages. But I feel like there must be some bit of shiny ruby or railness I'm still missing if this is the best I can do.
推荐答案
关于:
<%= tax_payment.user.name if tax_payment.user %>
这篇关于rails 如果 object.nil?然后魔术''在意见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文