3. REST, Resources, and Rails - 3.9 RESTful Route Customisations

3.9 RESTful Route Customisations

The techniques for doing this are useful when, for example, you’ve got more than one way of viewing a resource that might be described as showing. You can’t (or shouldn’t) use the show action itself for more than one such view. Instead, you need to think in terms of different perspectives on a resource, and create URLs for each one.

3.9.1 Etra Member Routes

For example, let’s say we want to make it possible to retract a bid. The basic nested route for bids looks like this:

resources :auctions do
  resources :bids
end

We’d like to have a retract action that shows a form (and perhaps does some screening for retractability). The retract isn’t the same as destroy; it’s more like a portal to destroy. It’s similar to edit, which serves as a form portal to update. Following the parallel with edit/update, we want a URL that looks like:

/auctions/3/bids/5/retract

and a helper method called retract_auction_bid_url. The way you achieve this is by specifying an extra member route for the bids.

resources :auctions do 
  resources :bids do
    member do
      get :retract
      post :retract
      # match :retract, via: [:get, :post]
    end 
  end
end

resources: auctions do 
  resources :bids do
    match :retract, via: [:get, :post], on: :member 
  end
end

3.9.2 Extra Collection Routes

You can use the same routing technique to add routes that conceptually apply to an entire collection of resources:

resources :auctions do 
  collection do
    match :terminate, via: [:get, :post] 
  end
end

In its shorter form:

resources :auctions do
  match :terminate, via: [:get, :post], on: :collection
end

This example will give you a terminate_auctions_path method, which will produce a URL mapping to the terminate action of the auctions controller. (A slightly bizarre example, perhaps, but the idea is that it would enable you to end all auctions at once.)

Thus you can fine-tune the routing behavior—even the RESTful routing behavior—of your application, so that you can arrange for special and specialized cases while still thinking in terms of resources.

3.9.3 Custom Action Names

Occasionally, you might want to deviate from the default naming convention for Rails RESTful routes. The :path_names option allows you to specify alternate name mappings. The example code shown changes the new and edit actions to Spanish-language equivalents.

resources :projects, path_names: {new: 'nuevo', edit: 'cambiar'}

3,9,4 Mapping to a Different Controller

You may use the :controller option to map a resource to a different controller than the one it would do so by default. This feature is occasionally useful for aliasing resources to a more natural controller name.

resources :photos, controller :"images"

3.9.5 Routes for New Resources

The routing system has a neat syntax for specifying routes that only apply to new resources, ones that haven’tbeen saved yet. You declare extra routes inside of a nested new block, like this:

resources :reports do 
  new do
    post :preview 
  end
end

The declaration above would result in the following route being defined.
preview_new_report POST /reports/new/preview(.:format) reports#preview

3.9.6 Considerations foro Extra Routes

Referring to extra member and collection actions, David has been quoted as saying, “If you’re writing so many additional methods that the repetition is beginning to bug you, you should revisit your intentions. You’re probably not being as RESTful as you could be.”

The last sentence is key. Adding extra actions corrupts the elegance of your overall RESTful application design, because it leads you away from finding all of the resources lurking in your domain.

Keeping in mind that real applications are more complicated than code examples in a reference book, let’s see what would happen if we had to model retractions strictly using resources. Rather than tacking a retract action onto the BidsController, we might feel compelled to introduce a retraction resource, associated with bids, and write a RetractionController to handle it.

resources :bids do
  resource :retraction 
end

`RetractionController` could now be in charge of everything having to do with retraction activities, rather than having that functionality mixed into `BidsController`. And if you think about it, something as weighty as bid retraction would eventually accumulate quite a bit of logic. Some would call breaking it out into its own controller proper separation of concerns or even just good object-orientation.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蹬癌,一起剝皮案震驚了整個濱河市土榴,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌逞频,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡蜈块,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進(jìn)店門迷扇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來百揭,“玉大人,你說我怎么就攤上這事蜓席∑饕唬” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵瓮床,是天一觀的道長盹舞。 經(jīng)常有香客問我产镐,道長,這世上最難降的妖魔是什么踢步? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任癣亚,我火速辦了婚禮,結(jié)果婚禮上获印,老公的妹妹穿的比我還像新娘述雾。我一直安慰自己,他們只是感情好兼丰,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布玻孟。 她就那樣靜靜地躺著,像睡著了一般鳍征。 火紅的嫁衣襯著肌膚如雪黍翎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天艳丛,我揣著相機(jī)與錄音匣掸,去河邊找鬼。 笑死氮双,一個胖子當(dāng)著我的面吹牛碰酝,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播戴差,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼送爸,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了暖释?” 一聲冷哼從身側(cè)響起袭厂,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎饭入,沒想到半個月后嵌器,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肛真,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡谐丢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蚓让。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乾忱。...
    茶點(diǎn)故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖历极,靈堂內(nèi)的尸體忽然破棺而出窄瘟,到底是詐尸還是另有隱情,我是刑警寧澤趟卸,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布蹄葱,位于F島的核電站氏义,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏图云。R本人自食惡果不足惜惯悠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望竣况。 院中可真熱鬧克婶,春花似錦、人聲如沸丹泉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽摹恨。三九已至筋岛,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間晒哄,已是汗流浹背泉蝌。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留揩晴,地道東北人勋陪。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像硫兰,于是被迫代替她去往敵國和親诅愚。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評論 2 355

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