Action Pack: Multiview
Rails 2.0: It's done!
にのっていた一部を英語苦手だけど訳してみるよ。
Multiviewを改善しました。#respond_toは以前からありましたが、この機能はテンプレートに対してさらに一歩踏み込み、テンプレートのフォーマットそれぞれにレンダリングエンジンを切り替えます。んで、show.rhtmlはshow.html.erbとなります。show.html.erbはrespondo_toが入っているformat.htmlを宣言し、デフォルトのshow actionをレンダリングするテンプレートです。もし、show.csv.erbがあったとき、これをtext/csvをデフォルトのerbレンダラーとして使います。
新しい形式のテンプレートのフォーマットは action.format.rendererです。
例:
show.erb: 同じ形式で表示するテンプレートすべて
index.atom.builder: 以前のrxmlのように、ビルダーのフォーマットを指定。application/atom+xmlのmime typeをレンダリングする。
edit/iphone.haml: カスタムhamlのテンプレートを使用して(デフォルトには含まれない)Mime::IPHONEのフォーマットでeditアクションをレンダリングする。
iPhoneについて話すと、簡単に宣言した”偽”のタイプを内部のルーティングのみで使用しています。iPhone用の特別なHTMLインターフェースが欲しいときに使えます。
こんな感じ:
# should go in config/initializers/mime_types.rb Mime.register_alias "text/html", :iphone class ApplicationController < ActionController::Base before_filter :adjust_format_for_iphone private def adjust_format_for_iphone if request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(iPhone|iPod)/] request.format = :iphone end end end class PostsController < ApplicationController def index respond_to do |format| format.html # renders index.html.erb format.iphone # renders index.iphone.erb end end end
config/initializers/mime_types.rbファイルにオリジナルのmime-typeを宣言します。このファイルはRails2.0のすべてのアプリケーションに含まれます。