2
在我的控制,我有:有沒有辦法停止執行調用方法來避免Rails中的DoubleRenderError?
def update
case params[:something]
when 'x'
if all_is_good
good_stuff_happens
flash[:notice] = "Good stuff happened"
else
access_denied
end
when 'y'
other_good_stuff_happens
flash[:notice] = "Other good stuff happened"
when 'z'
even_more_good_stuff_happens
flash[:notice] = "Even more good stuff happened"
end
redirect_to good_place_path
end
在我的ApplicationController我:
def access_denied
redirect_to message_path, :alert => 'Access Denied'
end
在大多數情況下,我總是想redirect_to的case語句後good_place_path。
偶爾,我想拒絕訪問權限並將其留在那。有沒有一種方法,我可以叫
acccess_denied
從我的控制器
再沒有返回給呼叫控制器(或者它試圖做了第二次重定向,因此我得到的DoubleRenderError)。我知道我可以只把
redirect_to good_place_path
在每個when語句,但我想知道是否有更優雅的解決方案。
謝謝。
肖恩
謝謝Steve.I試圖在access_denied方法的redirect_to之後直接返回,但它仍然是雙重渲染的,因爲它現在回到update方法並且仍在處理中。然後我嘗試了你的第二個建議,並且工作正常。我實際上結束了:access_denied &&返回 – 2010-11-05 01:10:15
是的,'rendering_method和return'在Rails中非常習慣。 – 2010-11-05 02:33:23