- 3. REST, Resources, and Rails -
- 3. REST, Resources, and Rails -
- 3. REST, Resources, and Rails -
- 3. REST, Resources, and Rails -
- 3. REST, Resources, and Rails -
- 3. REST, Resources, and Rails -
- 3. REST, Resources, and Rails -
- 3. REST, Resources, and Rails -
- 3. REST, Resources, and Rails -
- 3. REST, Resources, and Rails -
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:
- The routes for show, new, edit, and destroy are singular, because they’re working on a particular resource.
- 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:
- The action that results in the display of the form
- 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]
网友评论