所以我構建了一個身份驗證方法,它充當控制器中其他方法的before過濾器。基本上,如果用戶未登錄,我希望方法重定向到根路徑。以下是我的authenticate_user!前過濾方法:Rails控制器方法在Ajax調用後不重新載入頁面
def authenticate_user!
unless current_user
flash[:notice] = "Your session has ended. Please login again."
render js: "window.location.pathname = '#{root_path}'"
end
end
我遇到的問題是,窗口本身不重裝,而是在頁面中,並與文本內容元素顯示:「window.location.pathname =‘/’ 」。我無法使用redirect_to(我發現similar question here),因爲它似乎只是將某個內容元素髮送回root_path(而導航欄等元素保持不變)。這就是爲什麼我一直在尋找一個完整的窗口重新加載。任何幫助將非常感激。
編輯與服務器日誌:
Started GET "/projects/editHeader?fileName=test.csv" for 127.0.0.1 at 2014-07-04 10:37:21 -0400
Processing by ProjectsController#editHeader as TEXT
Parameters: {"fileName"=>"test.csv"}
Filter chain halted as :authenticate_user! rendered or redirected
Completed 200 OK in 2ms (Views: 0.1ms | ActiveRecord: 0.0ms)
Started GET "/projects/headerDataPreview?fileId=NaN" for 127.0.0.1 at 2014-07-04 10:37:21 -0400
Processing by ProjectsController#headerDataPreview as TEXT
Parameters: {"fileId"=>"NaN"}
Filter chain halted as :authenticate_user! rendered or redirected
Completed 200 OK in 1ms (Views: 0.1ms | ActiveRecord: 0.0ms)
這個解決方案似乎沒有任何改變;它只是將屏幕帶到一個空白頁面,並顯示文本「window.location ='\'」。 –
你可以發佈你的服務器日誌嗎?在我目前的項目中,我使用相同的代碼,它工作正常。 – Monideep
當然,我已經更新了日誌。 –