3. REST, Resources, and Rails - 3.5 The Standard RESTful Controller Actions

3.5 The Standard RESTful Controller Actions

Calling resources :auctions involves striking a kind of deal with the routing system. The system hands youfour named routes.

Helper Method GET POST PATCH DELETE
client_path(client) /clients/1 show - /clients/1 update /clients/1 destroy
clients_path /clients index /clients create - -
edit_client_path(client) /clients/1/edit edit - - -
new_client_path /clients/new new - - -

You can specify a DELETE operation by:

link_to "Delete", auction_path(action), method: :delete
# or
form_for "auction", url: auction_path(auction),
  html: {method: :path} do |f|

3.5.1. PATCH vs. PUT

In the HTTP standards document RFC 57892, it outlines that a PUT request to a given resource is meant to completely replace it on the origin server.

3.5.2 Singular and Plural RESTful Routes

The logic is as follows:

  1. The routes for show, new, edit, and destroy are singular, because they’re working on a particular resource.
  2. The rest of the routes are plural. They deal with collections of related resources.

The singular RESTful routes require an argument, because they need to be able to figure out the id of the member of the collection referenced.

item_url(item) # show, update, or destroy, depending on HTTP verb

You don’t have to call the id method on item. Rails will figure it out (by calling to_param on the object passed
to it.)

3.5.3 The Special Pairs new/create and edit/update

Typically, create and update operations involve submitting a form. That means that they really involve two actions—two requests—each:

  1. The action that results in the display of the form
  2. The action that processes the form input when the form is submitted

The way this plays out with RESTful routing is that the create action is closely associated with a preliminary new action, and update is associated with edit. These two actions, new and edit, are really assistant actions: All they’re supposed to do is show the user a form, as part of the process of creating or updating a resource.

3.5.4 The PATCH and DELETE Cheat

Some HTTP clients are able to use saidverbs, but forms in web browsers can’t be submitted using anything other than a POST. Rails provides a hackthat is nothing to worry about, other than being aware of what’s going on.

A PATCH or DELETE request originating in a browser, in the context of REST in Rails, is actually a POST request with a hidden field called _method set to either "patch" or "delete".

3.5.5

It’s possible to add :except and :only options to the call to resources in order to limit the routes generated.

resources:clients,except:[:index]
resources:clients,only:[:new,:create]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343

推荐阅读更多精彩内容

  • 亲爱的粑粑麻麻,逼孩子做他不想做的事情,这个过程,是教育吗? 比如学习,你去试一下那感觉,你也不想学! 当你自己还...
    瑜伽生活cc阅读 401评论 0 0
  • 从阳台上摔下来的时候,那一刻,我是真的以为自己就要那么挂掉了,什么都没抓住,大脑一片空白。奇怪的是并没有感到多大的...
    虞七七阅读 153评论 0 6
  • 那棵玉兰树 又悄悄地开了 不动声色 没有惊动任何人 我自树下走过 玉兰里的精灵留住了我 她用她幽怨的香气向我诉说 ...
    执年就是依晗曦阅读 171评论 0 3
  • ​1、所谓战略,就是站在未来看今天“,但什么是明天的样子?恰恰要分析昨天,然后下注明天,然后做好今天——曾鸣 2、...
    甲基橙579阅读 428评论 0 0
  • 昨天今天明天 作词:卢木鸟 是谁放弃了昨日的月光 昨日的生活变成了假想 是去年寄托了今年的梦想 岁月无法回到童年的...
    華狐发布阅读 138评论 0 0