3. REST, Resources, and Rails - 3.11 Different Representations of Resources

3.11 Different Representations of Resources

One of the precepts of REST is that the components in a REST-based system exchange representations of resources. The distinction between resources and their representations is vital.

As a client or consumer of REST services, you don’t actually retrieve a resource from a server; you retrieve representations of that resource. You also provide representations: A form submission, for example, sends the server a representation of a resource, together with a request—for example, PATCH—that this representation be used as the basis for updating the resource. Representations are the exchange currency of resource management.

3.11.1 The respond_to Method

The ability to return different representations in RESTful Rails practice is based on the respond_to method in the controller, which, allows you to return different responses depending on what the client wants. Moreover, when you create resource routes you automatically get URL recognition for URLs ending with a dot and a :format parameter.

For example, assume that you have resources :auctions in your routes file and some respond_to logic in the AuctionsController like:

def index
  @auctions = Auction.all 
  respond_to do |format|
    format.html
    format.xml { render :xml => @auctions } 
  end
end

which will let you to connect to this URL: /auctions.xml
The resource routing will ensure that the index action gets executed. It will also recognize the .xml at the end of the route and interact with respond_to accordingly, returning the XML representation.

There is also a more concise way of handling this now using the respond_with method.

class AuctionsController < ApplicationController 
  respond_to :html, :xml, :json
  def index
    @auctions = Auction.all
    respond_with(@auctions) 
  end
end

Here we’ve told our controller to respond to html,xml, and json so that each action will automatically return the appropriate content. When the request comes in, the responder would attempt to do the following given a .json extension on the URL:

  • Attempt to render the associated view with a .jsonextension.
  • If no view exists,call to_json on the object passed to responds_with.
  • If the object does not respond to to_json,call to_formaton it.

For nested and namespaced resources, simply pass all the objects to the respond_to method similar to the way you would generate a route.

respond_with(@user, :managed, @client)

3.11.2 Formatted Named Routes

Let’s say you want a link to the XML representation of a resource. You can achieve it by passing an extra argument to the RESTful named route:

link_to "XML version of this aution", auction_path(@auction, :xml)

# this will generate the following HTML:
# <a href="/auctions/1.xml">XML version of this auction</a>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末构挤,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子脱羡,更是在濱河造成了極大的恐慌混卵,老刑警劉巖赊瞬,帶你破解...
    沈念sama閱讀 212,884評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件戒突,死亡現(xiàn)場(chǎng)離奇詭異氯迂,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)言缤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,755評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門嚼蚀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人管挟,你說(shuō)我怎么就攤上這事轿曙。” “怎么了僻孝?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,369評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵导帝,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我穿铆,道長(zhǎng)您单,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,799評(píng)論 1 285
  • 正文 為了忘掉前任荞雏,我火速辦了婚禮虐秦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘凤优。我一直安慰自己悦陋,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,910評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布筑辨。 她就那樣靜靜地躺著叨恨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪挖垛。 梳的紋絲不亂的頭發(fā)上痒钝,一...
    開(kāi)封第一講書(shū)人閱讀 50,096評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音痢毒,去河邊找鬼送矩。 笑死,一個(gè)胖子當(dāng)著我的面吹牛哪替,可吹牛的內(nèi)容都是我干的栋荸。 我是一名探鬼主播,決...
    沈念sama閱讀 39,159評(píng)論 3 411
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼凭舶,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼晌块!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起帅霜,我...
    開(kāi)封第一講書(shū)人閱讀 37,917評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤匆背,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后身冀,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體钝尸,經(jīng)...
    沈念sama閱讀 44,360評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡括享,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,673評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了珍促。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片铃辖。...
    茶點(diǎn)故事閱讀 38,814評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖猪叙,靈堂內(nèi)的尸體忽然破棺而出娇斩,到底是詐尸還是另有隱情,我是刑警寧澤穴翩,帶...
    沈念sama閱讀 34,509評(píng)論 4 334
  • 正文 年R本政府宣布成洗,位于F島的核電站,受9級(jí)特大地震影響藏否,放射性物質(zhì)發(fā)生泄漏瓶殃。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,156評(píng)論 3 317
  • 文/蒙蒙 一副签、第九天 我趴在偏房一處隱蔽的房頂上張望遥椿。 院中可真熱鬧,春花似錦淆储、人聲如沸冠场。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,882評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)碴裙。三九已至,卻和暖如春点额,著一層夾襖步出監(jiān)牢的瞬間舔株,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,123評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工还棱, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留载慈,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,641評(píng)論 2 362
  • 正文 我出身青樓珍手,卻偏偏與公主長(zhǎng)得像办铡,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子琳要,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,728評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容