在我的軌位指示我有這樣的代碼:跳過`if`的情況下,這種說法是不可能的
if Photo.find(params[:photo_id]).patient_id == nil
.........
else
.........
當時我得到一個錯誤
所以當沒有params[:photo_id]
:
Couldn't find Photo without an ID
我知道,我可以解決這個問題:例如:
if(params[:photo_id]) && (Photo.find(params[:photo_id]).patient_id == nil)
但現在我試着寫一個異常「如何其所謂的我THI NK「:
if Photo.find(params[:photo_id]).patient_id == nil rescue false
拋出一個新的錯誤:
syntax error, unexpected modifier_rescue, expecting keyword_then or ';' or '\n' if
那麼做了一個錯誤的(我在編程初學者!)
反模式:'rescue false'將拯救*行中的任何*錯誤 - 從RecordNotFounds到NameErrors或錯別字,後續源代碼的編輯介紹...最好不要這樣做;-) – Pavling