The three (conceptual) actions that modify a database table are CREATE, MODIFY and DELETE. The first two -- CREATE and MODIFY -- are handled nicely in the RESTful framework, the DELETE action is not. To explain:
The contract of #new is to respond with a form that when submitted calls #create to modify the database. The #new request is idempotent and can use GET, the #create method actually modifies the database and uses POST.
Similarly, the contract of #edit is to generate a form that when submitted, calls #update to modify the database. The #edit request is idempotent, the #update method modifies the database.
The Rails framework would benefit from the addition of a #delete method (and route), whose contract is to generate a form that, when submitted, calls #destroy, along the same lines as the #new/#create and #edit/#update methods.
+1 for this.
I always do this manually, every time. It would be better if the generators could generate it automatically, along with the views, and it be included in resources route.
Yes, this would be an awesome plugin. Rails has very customizable generators since version 3.0 and you should be able to easily implement this.
@josevalim: Are you asserting that this should NOT become part of the core Rails distro? Or -- just as beatification is a necessary step towards sainthood -- is a plugin a necessary step towards integration into the language?
I guess he means that it is a feature request and not an issue. I do not know if there is a good place for discussing rails feature requests.
Is this not a good place for feature requests? or should this be posted in rails core newsgroup instead?
The comments further support the need for this kind of functionality.
@nbt: I agree with @josevalim. This shouldn't be included into rails core.
It's a specific need, dependent of some user interfaces. Moreover, it's one which is easily fixable.
resources :post do
It would be cool as a gem though.
Is progressive enhancement/graceful degradation dead?
I don't see why we really need this. If you call Rails a "RESTful frameworks", then the correct way to delete a resource is to send DELETE /:resources/:id. Since browsers are stupid, we had to use a form to submit a POST request with _method=delete.
BTW, it's generally a bad idea anyway to allow user to issue a GET request to destroy a resource. If you think you really want the route, just go ahead and create one in your application yourself. If you're using Rails 4, consider using a Routing Concern to do that as well.
I'm not suggesting a 'delete' action that deletes the resource, that would be stupid. The 'delete' action simply shows a confirmation form that posts the the 'destroy' action with '_method=delete', that's all. So 'delete' is to 'destroy' what 'edit' is to 'update'.
Since browsers are stupid,
Since browsers are stupid,
Actually, browsers are smart: they follow the HTML spec exactly, which says that FORMs can only support GET and POST. Using POST to delete something is just fine according to REST: http://roy.gbiv.com/untangled/2009/it-is-okay-to-use-post
This is why it's important to separate "What Rails does" from REST.