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]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 親愛的粑粑麻麻,逼孩子做他不想做的事情,這個過程,是教育嗎? 比如學(xué)習(xí),你去試一下那感覺,你也不想學(xué)! 當(dāng)你自己還...
    瑜伽生活cc閱讀 455評論 0 0
  • 從陽臺上摔下來的時候,那一刻,我是真的以為自己就要那么掛掉了,什么都沒抓住,大腦一片空白。奇怪的是并沒有感到多大的...
    虞七七閱讀 195評論 0 6
  • 那棵玉蘭樹 又悄悄地開了 不動聲色 沒有驚動任何人 我自樹下走過 玉蘭里的精靈留住了我 她用她幽怨的香氣向我訴說 ...
    執(zhí)年就是依晗曦閱讀 205評論 0 3
  • ?1、所謂戰(zhàn)略,就是站在未來看今天“,但什么是明天的樣子?恰恰要分析昨天,然后下注明天,然后做好今天——曾鳴 2、...
    甲基橙579閱讀 535評論 0 0
  • 昨天今天明天 作詞:盧木鳥 是誰放棄了昨日的月光 昨日的生活變成了假想 是去年寄托了今年的夢想 歲月無法回到童年的...
    華狐發(fā)布閱讀 182評論 0 0

友情鏈接更多精彩內(nèi)容