最近在使用RoR做項(xiàng)目,體會(huì)到了快速開(kāi)發(fā)的樂(lè)趣,也遇到了一些困難,其中一個(gè)就是redirect_to。
我遇到的一個(gè)問(wèn)題是,當(dāng)使用Ajax更新頁(yè)面局部?jī)?nèi)容時(shí),session內(nèi)容已經(jīng)過(guò)期,這時(shí)需要整個(gè)頁(yè)面跳轉(zhuǎn)到登錄頁(yè)面。
直接調(diào)用redirect_to會(huì)使局部?jī)?nèi)容顯示成登錄頁(yè)面,它是在HTTP頭里寫入重定向參數(shù)來(lái)實(shí)現(xiàn)的。在我這里的特殊情況下,正確的做法是讓它執(zhí)行一個(gè)包含在<script>標(biāo)記中的腳本,在腳本中更改窗口location值來(lái)跳轉(zhuǎn)。
不過(guò)RoR中使用Ajax時(shí),會(huì)根據(jù):update參數(shù)來(lái)決定是使用Updater還是Request。如果使用Updater方式,則應(yīng)返回一段純腳本;如果是Request方式,應(yīng)返回一段包括在<script>標(biāo)記中的腳本;如果是普通方式,就應(yīng)該使用原有的redirect_to函數(shù)了。因?yàn)榉?wù)端無(wú)法區(qū)分使用的是哪種方式來(lái)請(qǐng)求,所以簡(jiǎn)單的做法是每個(gè)請(qǐng)求都附加一個(gè)參數(shù)用來(lái)區(qū)分,不加參數(shù)則是普通請(qǐng)求方式。
為了達(dá)到這個(gè)目的,我修改了prototype_helper中的remote_function函數(shù)。這個(gè)函數(shù)根據(jù)傳遞進(jìn)來(lái)的參數(shù)來(lái)決定使用Request或是Updater,我就在這里下手:
有紅色的2行是我添加的,由于這個(gè)編輯器的原因,沒(méi)有顯示成整行紅色。這2行的作用是判斷是否有:update參數(shù),用它來(lái)決定是添加ajax=update還是ajax=request。
現(xiàn)在可以實(shí)現(xiàn)一個(gè)簡(jiǎn)單的auto_redirect_to了:
使用helper方式使它能夠被include到ApplicationController中就行了。
為了不和參數(shù)綁得太死,這里把method作為參數(shù)由調(diào)用者傳入。
使用方法,以Login Engine為例,它在access_denied中處理跳轉(zhuǎn)。在ApplicationController中重寫這個(gè)函數(shù):
現(xiàn)在可以測(cè)試了。請(qǐng)求可以是普通的(超鏈接),Updater方式(請(qǐng)求到一個(gè)DIV里),Request方式,現(xiàn)在都能夠跳轉(zhuǎn)到正確頁(yè)面。
ajax參數(shù)通過(guò)hack庫(kù)代碼來(lái)實(shí)現(xiàn),對(duì)于使用者來(lái)說(shuō)基本上是透明的。
我遇到的一個(gè)問(wèn)題是,當(dāng)使用Ajax更新頁(yè)面局部?jī)?nèi)容時(shí),session內(nèi)容已經(jīng)過(guò)期,這時(shí)需要整個(gè)頁(yè)面跳轉(zhuǎn)到登錄頁(yè)面。
直接調(diào)用redirect_to會(huì)使局部?jī)?nèi)容顯示成登錄頁(yè)面,它是在HTTP頭里寫入重定向參數(shù)來(lái)實(shí)現(xiàn)的。在我這里的特殊情況下,正確的做法是讓它執(zhí)行一個(gè)包含在<script>標(biāo)記中的腳本,在腳本中更改窗口location值來(lái)跳轉(zhuǎn)。
不過(guò)RoR中使用Ajax時(shí),會(huì)根據(jù):update參數(shù)來(lái)決定是使用Updater還是Request。如果使用Updater方式,則應(yīng)返回一段純腳本;如果是Request方式,應(yīng)返回一段包括在<script>標(biāo)記中的腳本;如果是普通方式,就應(yīng)該使用原有的redirect_to函數(shù)了。因?yàn)榉?wù)端無(wú)法區(qū)分使用的是哪種方式來(lái)請(qǐng)求,所以簡(jiǎn)單的做法是每個(gè)請(qǐng)求都附加一個(gè)參數(shù)用來(lái)區(qū)分,不加參數(shù)則是普通請(qǐng)求方式。
為了達(dá)到這個(gè)目的,我修改了prototype_helper中的remote_function函數(shù)。這個(gè)函數(shù)根據(jù)傳遞進(jìn)來(lái)的參數(shù)來(lái)決定使用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
????????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行是我添加的,由于這個(gè)編輯器的原因,沒(méi)有顯示成整行紅色。這2行的作用是判斷是否有:update參數(shù),用它來(lái)決定是添加ajax=update還是ajax=request。
現(xiàn)在可以實(shí)現(xiàn)一個(gè)簡(jiǎn)單的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
????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中就行了。
為了不和參數(shù)綁得太死,這里把method作為參數(shù)由調(diào)用者傳入。
使用方法,以Login Engine為例,它在access_denied中處理跳轉(zhuǎn)。在ApplicationController中重寫這個(gè)函數(shù):
????def?access_denied
??????auto_redirect_to(params[:ajax],?:controller?=>?"/user",?:action?=>?"login")
??????false
????end??
??????auto_redirect_to(params[:ajax],?:controller?=>?"/user",?:action?=>?"login")
??????false
????end??
現(xiàn)在可以測(cè)試了。請(qǐng)求可以是普通的(超鏈接),Updater方式(請(qǐng)求到一個(gè)DIV里),Request方式,現(xiàn)在都能夠跳轉(zhuǎn)到正確頁(yè)面。
ajax參數(shù)通過(guò)hack庫(kù)代碼來(lái)實(shí)現(xiàn),對(duì)于使用者來(lái)說(shuō)基本上是透明的。