プログレッシブニート日記

HoLY Diary

このページについて

その日の日記,備忘録,URLメモといった類の(おそらく内容が無い)文章を書いています.


2005.12.15(Thu)

_ after_filter と before_filter

before_filter で失敗すると after_filter を実行しない.

……のは分かるんだけど,結構困ることはあるんだよなぁ.before_filter 内でfalseを返すたびに after_filterの内容を書いておくとかだるい.どうしたらいいんだろう.(とりあえず1箇所くらいしか書くところないので放置)

Tags: Rails Ruby

_ 携帯用viewを書いたり書かなかったり

user-agentによって表示するテンプレートを変更する を見つつapplication.rbをいじってみるよ.

 after_filter :output_filter_for_mobile_agent
 
 # :
 # :
 
 def output_filter_for_mobile_agent
   
   unless mobile?(@request)
     return
   end
   
   new_template = @template.first_render + '.mobile'
   new_layout = active_layout + '.mobile'
   # redirect なら変更しない
   if !@performed_redirect
     if template_exists?(new_template) or template_exists?(new_layout)
       
       unless template_exists?(new_template)
         new_template = @template.first_render
       end
       unless template_exists?(new_layout)
         new_layout = active_layout
       end
       
       # render結果を削除
       erase_render_results
       
       logger.info "template => #{new_template}, layout => #{new_layout}"
       render :template => new_template, :layout => new_layout
     else
       logger.warn "template '#{r_new}' doesn't exists."
     end
   end
   
   # 文字コードは Shift_JIS
   @response.body = NKF::nkf('-s', @response.body)
 end

render :template => welcome/index だったときに welcome/index.mobile.rhtml があればそれを参照するようにする.(layoutも同様)

mobile? は他の場所で定義してるけどこんな感じ.

 MOBILE_USER_AGENT = %r{ DoCoMo
   |J-PHONE
   |UP.Browser
   |DDIPOCKET
   |ASTEL
   |PDXGW
   |Palmscape
   |Xiino
   |sharp pda browser
   |MOT-
   |Windows CE
   |L-mode
   |w3m }xi
 
 def mobile?(request)
   request.env['HTTP_USER_AGENT'] =~ MOBILE_USER_AGENT
 end
Tags: Rails Ruby
本日のリンク元
アンテナ
検索

Auther: HoLY <holy@enyou.org>