respond_to 與 respond_with

respond_to可以讓你根據客戶端要求的格式進行不同的格式回應,以 RESTful 與 Rails 中完成的應用程式首頁為例,若想要客戶端在請求http://localhost:3000/bookmarks.html、http://localhost:3000/bookmarks.xml、http://localhost:3000/bookmarks.json時,分別給HTML、XML、JSON格式回應,可以如下修改:

bookmarks_controller.rb
class BookmarksController < ApplicationController
    def index
        @pages = Page.all
        respond_to do |format|
            format.html
            format.xml { render : xml => @pages }
            format.json { render :json => @pages }
        end
    end
    ...
end
上例中,respond_to傳入的區塊參數format是ActionController::MimeResponds::Collector實例,程式區塊中format.html表示如果是.html結尾請求,執行預設的render方法,format.xml、format.json表示如果是.xml或.json,執行方法後程式區塊的內容,依範例程式則是分別是指定呈現XML與JSON格式。如果想要設定預設處理,可以使用format.any方法。例如:

respond_to do |format|
    format.html
    format.xml { render : xml => @pages }
    format.json { render :json => @pages }
    format.any { render :text => "Orz" }
end
實際上,如果要整個控制器可依客戶端請求格式進行回應,可以如下:

class SomesController < ApplicationController
    respond_to :html, : xml, :json

    def index
        ...
        respond_with(Some.all)
    end
end

在類別層級宣告控制器支援的格式,然後動作中使用respond_with,指定要呈現的物件,就會自動客戶端請求的格式,傳回對應的格式回應。respond_with也接受程式區塊,用以重新定義預設的render行為。例如:
respond_with(Some.all) do |format|
    format.html { render }
end
respond_to或respond_with的區塊參數format,如果format.html後沒有設定程式區塊,預設會尋找index.html.erb進行回應,同理,如果format.xml或format.json沒有設定程式區塊,預設會尋找index.xml.erb或index.json.erb進行回應。Erb是Embedded Ruby,也就是可內嵌Ruby程式碼的樣版檔案,你也可以使用Builder檔案,也就是副檔名為.builder的檔案。例如:

bookmarks_controller.rb
class BookmarksController < ApplicationController
    def index
        @pages = Page.all
        respond_to do |format|
            format.html
            format.xml
            format.json { render :json => @pages }
        end
    end
    ...
end
format.xml沒有設定程式區塊,因此優先尋找.xml.erb,如果沒有的話,再尋找.xml.builder,如果這邊僅設定index.xml.bulder如下:

index.xml.builder
xml.pages do                                  # 根節點 <pag es></pages>
    @pages.each do |page|
        xml.page do                           # 節點 <page></page>
           xml.title page.title               # 節點 <title></title>
           xml.url page.url                   # 節點 <url></url>
           xml.description page.description   # 節點 <description></description>
        end
    end
end
如上產生的XML會是:

<pages>
  <page>
    <title>Openhome</title>
    <url>http://openhome.cc</url>
    <description>Orzxxxxxxx</description>
  </page>
  <page>
    <title>xxx</title>
    <url>http://xxx</url>
    <description>測試</description>
  </page>
  ...
</pages>
Rails內建的格式支援包括有:html、:xml、:json、:text、:js、:css、:ics、:csv、:rss、:atom、:yaml等,可以編輯config/initializers/mime_types.rb來註冊新的格式支援。

猜你喜欢

转载自zhuoxiaolong.iteye.com/blog/1745051