The CRUD principle defines the four basic operations on persistent data:
HTTP verbs also use the DELETE word.
Why does the default routing in Rails use the word "destroy" for the action corresponding to the HTTP verb DELETE?
Basically destroy runs any callbacks on the model while delete doesn't. Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can't be persisted).
It's a way to redirect incoming requests to controllers and actions. It replaces the mod_rewrite rules. Best of all, Rails' Routing works with any web server. Routes are defined in app/config/routes.
Rails uses 4 standard methods(verbs), namely:
Besides it has 7 RESTful actions:
Rails never uses the same verb as the corresponding action. Routing to the action destroy makes it possible to do more than a single DELETE, through the corresponding action in the controller.
This railsguide might be of interest to you: http://guides.rubyonrails.org/routing.html
Explanation
Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as GET, POST, PUT and DELETE. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.
Now, imagine we have a HTTP GET request, which means you want to read/retrieve data. If the action would have the same name as the verb, GET in this case, it would be overly simplistic. GET can give access to show, index, new or edit actions. They all read data, but the actions themselves are definitely not the same. The same could be said about the DELETE request. This request is processed through the controller and can have different implementations within actions. It might be you want to destroy a post, but it might as well mean you want to log out of your user session. Only having an action called delete would not justify the possibilities related to it, through the controller.
Edit
If you want to know more about how requests from the browser are processed, you could read some information about the M(odel)V(iew)C(ontroller)-model that Rails uses:
http://www.youtube.com/watch?v=3mQjtk2YDkM&noredirect=1
and:
http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/
A quote from this link:
The browser makes a request, such as http://mysite.com/video/show/15 The web server (mongrel, WEBrick, etc.) receives the request. It uses routes to find out which controller to use: the default route pattern is “/controller/action/id” as defined in config/routes.rb.
Meaning your initial request will be translated and processed through the webserver and the correct route has to be defined through the controller, where the restful action, such as destroy, is located.
In the early days of Rails, there were only 2 verb's, namely GET and POST (since PUT and DELETE are not supported, which later versions of rails resolved by adding PUT and DELETE through hidden variables. The name of the destroy action never changed, since request and actions are two different things.
Actions || show || create || update || destroy SQL || select|| create || update || delete REST || get || post || post || post Actions || show || create || update || destroy SQL || select|| create || update || delete REST || get || post || put || delete
This quote may be of further interest:
"Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions."
http://guides.rubyonrails.org/routing.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With