respond_to ブロック
Action Controller のrespond_to ブロックは、フォーマットに応じた形でレスポンスを
返してくれる。
クライアントがHTTPのAcceptヘッダを指定するのが公式なフォーマットの指定方法だが、
単純に、URLの最後に、show/1.html、show/2.xmlとつける事で要求できる。
以下、html,xml,yamlで要求があった場合、それぞれのフォーマットで返却する。
def show respond_to do |format| format.html format.xml { render :xml => @product.to_xml } format.yaml { render :text => @product.to_yaml } end end
また、フォーマットの指定はHTTPのリクエストパラメータとして渡す事もできる。
http://……….com/yyy/show123?format=xml
デフォルトにないMIMEタイプの設定は、enviroment.rbに設定できるが、設定方法はまた別途。