最近在使用RoR做項目,體會到了快速開發的樂趣,也遇到了一些困難,其中一個就是redirect_to。
我遇到的一個問題是,當使用Ajax更新頁面局部內容時,session內容已經過期,這時需要整個頁面跳轉到登錄頁面。
直接調用redirect_to會使局部內容顯示成登錄頁面,它是在HTTP頭里寫入重定向參數來實現的。在我這里的特殊情況下,正確的做法是讓它執行一個包含在<script>標記中的腳本,在腳本中更改窗口location值來跳轉。
不過RoR中使用Ajax時,會根據:update參數來決定是使用Updater還是Request。如果使用Updater方式,則應返回一段純腳本;如果是Request方式,應返回一段包括在<script>標記中的腳本;如果是普通方式,就應該使用原有的redirect_to函數了。因為服務端無法區分使用的是哪種方式來請求,所以簡單的做法是每個請求都附加一個參數用來區分,不加參數則是普通請求方式。
為了達到這個目的,我修改了prototype_helper中的remote_function函數。這個函數根據傳遞進來的參數來決定使用Request或是Updater,我就在這里下手:
??????def?remote_function(options)
????????javascript_options?=?options_for_ajax(options)
????????update?=?''
????????if?options[:update]?and?options[:update].is_a?Hash
??????????update??=?[]
??????????update?<<?"success:'#{options[:update][:success]}'"?if?options[:update][:success]
??????????update?<<?"failure:'#{options[:update][:failure]}'"?if?options[:update][:failure]
??????????update??=?'{'?+?update.join(',')?+?'}'
????????elsif?options[:update]
??????????update?<<?"'#{options[:update]}'"
????????end
????????function?=?update.empty????
??????????"new?Ajax.Request("?:
??????????"new?Ajax.Updater(#{update},?"
????
????????url_options?=?options[:url]
????????ajax_options?=?options[:update]???{:ajax?=>?'update'}?:?{:ajax?=>?'request'}
????????url_options?=?url_options.merge(ajax_options)
????????url_options?=?url_options.merge(:escape?=>?false)?if?url_options.is_a??Hash
????????function?<<?"'#{url_for(url_options)}'"
????????function?<<?",?#{javascript_options})"
????????function?=?"#{options[:before]};?#{function}"?if?options[:before]
????????function?=?"#{function};?#{options[:after]}"??if?options[:after]
????????function?=?"if?(#{options[:condition]})?{?#{function};?}"?if?options[:condition]
????????function?=?"if?(confirm('#{escape_javascript(options[:confirm])}'))?{?#{function};?}"?if?options[:confirm]
????????return?function
??????end
有紅色的2行是我添加的,由于這個編輯器的原因,沒有顯示成整行紅色。這2行的作用是判斷是否有:update參數,用它來決定是添加ajax=update還是ajax=request。
現在可以實現一個簡單的auto_redirect_to了:
??def?auto_redirect_to(method,?url)
????case?method
????when?'request'
??????request_redirect_to(url)
????when?'update'
??????update_redirect_to(url)
????else
??????redirect_to(url)
????end
??end
??
??def?request_redirect_to(url)
????render?:update?do?|page|
??????page.redirect_to(url)
????end
??end
??
??def?update_redirect_to(url)
????render?:inline?=>?<<-EOS
??????<script?language="javascript">
??????<%=
????????render?:update?do?|page|
??????????page.redirect_to("#{url_for(url)}")
????????end
??????%>
??????</script>
????EOS
??end
使用helper方式使它能夠被include到ApplicationController中就行了。
為了不和參數綁得太死,這里把method作為參數由調用者傳入。
使用方法,以Login Engine為例,它在access_denied中處理跳轉。在ApplicationController中重寫這個函數:
????def?access_denied
??????auto_redirect_to(params[:ajax],?:controller?=>?"/user",?:action?=>?"login")
??????false
????end??
現在可以測試了。請求可以是普通的(超鏈接),Updater方式(請求到一個DIV里),Request方式,現在都能夠跳轉到正確頁面。
ajax參數通過hack庫代碼來實現,對于使用者來說基本上是透明的。