地名しりとりにフィードをつけた (2) (FeedTools)
地名しりとりにフィードをつける話の続き。 先日の日記で書いたとおり、キャッシュが有効だとヘッダの Content/Type が書き換わらないという問題があった。
フィルタを使ってあれこれ試した結果、 prepend_before_filter を使えばいいという結論になった。 それ以外のフィルタ before_filter, after_filter, prepend_before_filter だと、キャッシュが有効の時にはなぜか実行されない。
という訳で、既存のコントローラを prepend_before_filter を使うように修正した。
Index: app/controllers/histories_controller.rb =================================================================== --- app/controllers/histories_controller.rb (リビジョン 12) +++ app/controllers/histories_controller.rb (作業コピー) @@ -7,6 +7,7 @@ :add_flash => { :notice => '地名を選択してください'}, :only => [:create] caches_action :json, :feed + prepend_before_filter :feed_charset, :only => :feed # メイン画面を返す。何も処理はしない。 def index @@ -71,7 +72,12 @@ end def feed - headers['Content-Type'] = 'text/xml;charset=utf-8' render(:text => History.feed) end + +protected + + def feed_charset + headers['Content-Type'] = 'text/xml;charset=utf-8' + end
ついでに route.rb を修正した。 http://capping.machu.jp/feed.xml でフィードを取得できるようにしている。
Index: config/routes.rb =================================================================== --- config/routes.rb (リビジョン 8) +++ config/routes.rb (作業コピー) @@ -12,6 +12,7 @@ # You can have the root of your site routed by hooking up '' # -- just remember to delete public/index.html. map.connect '', :controller => "histories" + map.connect 'feed.xml', :controller => "histories", :action => 'feed' # Allow downloading Web Service WSDL as a file with an extension # instead of a file named 'wsdl' Index: app/views/histories/index.rhtml =================================================================== --- app/views/histories/index.rhtml (リビジョン 12) +++ app/views/histories/index.rhtml (作業コピー) @@ -6,7 +6,7 @@ <title>地名しりとり</title> <link href="http://www.google.com/uds/css/gsearch.css" rel="stylesheet" type="text/css"/> <link href="/stylesheets/capping.css" rel="stylesheet" type="text/css"/> -<link rel="alternate" type="application/rss+xml" title="RSS" href="/histories/feed"> +<link rel="alternate" type="application/rss+xml" title="RSS" href="feed.xml"> <script type="text/javascript" src="/javascripts/prototype.js"></script> <script src="http://maps.google.co.jp/maps?file=api&v=2&key=<%=h GOOGLE_MAPS_API_KEY %>" type="text/javascript"></script> <script src="http://www.google.com/uds/api?file=uds.js&v=1.0&key=<%=h GOOGLE_AJAX_SEARCH_API_KEY %>" type="text/javascript"></script>
これで livedoor Reader でフィードを拾ってくれるようになるといいな。
追記
なんか拾ってくれているっぽい。