我在整个应用程序中都有语句,如@user = User.find(current_user.id)
。优雅地转义错误
有时用户可能会输入一个零变量(例如current_user为零的新用户)。
我敢肯定,这样做将是散射哑方式,如果喜欢到处发言...
if current_user.exists?
@user = User.find(current_user.id)
else
redirect_to root_url
---*or*---
@user = "new" # for use with if/case statements later on
end
什么是优雅的方式来应对这种混乱呢?
太棒了!谢谢Geoff。 – sscirrus 2010-07-15 18:49:15