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]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

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

友情链接更多精彩内容