我在整個應用程序中都有語句,如@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