unless current_user.owns_photo? @photo
redirect_to photos_path
end
我有這樣的代碼,當我運行該應用程序,如果當前照片不屬於所有者它將重定向用戶照片的路徑。但是,當我運行相同的一組代碼,但添加回報:redirect_to無法正常工作
unless current_user.owns_photo? @photo
redirect_to photos_path && return
end
它會導致不規則的結果。經過身份驗證的用戶可以繼續查看不屬於他們的內容。但是,當我放棄返回它工作正常。爲什麼它不與回報一起工作?我聽說在使用redirect_to時返回是很好的做法,因此它不會在後面執行更多代碼。
def owns_photo?(photo)
photos.include? photo
end
這是owns_photo的方法工作正常。我只是好奇這個回報問題。
這是很好的幫助謝謝 – Byrd