#77 Destroy without JavaScript (revised)
A destroy link in Rails does not work without JavaScript. In this episode I show two techniques for gracefully degrading destroy links: button_to and a delete action.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
This doesn't seem like a good solution to me. Overwriting the "resources" method in an initializer will confuse the heck out of anyone looking at your code except yourself.
I'd say if you need to support users with JS disabled, use a button (and don't make it look like a link either!)
+1, but one can still learn from this approach even though its wrong
Why would this be wrong? Another user would see the delete method in the controllers and hopefully be able to discover the initializer. No?
Nothing wrong with this approach I think. Changing rails code is perfectly fine, though documented if necessary, I'd say. Nothing wrong with extending/improving rails itself. ;-)
This means all resources will have a delete action on them.
Isn't that a bad idea?
If this is how you handle deletion application wide, then I don't see a problem with it. All resources already have a destroy action.
DELETE /products/1/delete
looks rather confusing, I think it is better to use POST verb in this case:POST /products/1/delete
. Also, these are perfectly valid RESTful routes, there is nothing wrong with them.I would also suggest to extend your routing DSL explicitly instead of patching Rails internal classes so that the modification is obvious for you and others. This will also avoid troubles with external engines that will expect default behavior.
Very useful. Thanks, Ryan.
I did have to change a few things before it worked in Rails 3.0 though. I had to remove the html hash in the delete form.
<%= form_tag request.url, method: :delete do %>
And I needed to add a delete method to the application_controller:
After that it worked beautifully.
Hi,
that was one thing I didn't get - I was missing the action in the application controller.
However, you shouldn't use render here - you limit yourself. The way Ryan is doing it here gives you the opportunity to "overwrite" the template by putting another version of the template into the specific controllers view directory.
How would we do to do it remotely ?
like :remote => true
I still maintain that you should be able to delete records in Rails by default when Javascript is not available, I've been banging on about this since 2006:
I must get around to implementing it as an Engine. I find it frustrating that you have to include the whole of jQuery on an app that doesn't otherwise require Javascript, just to let users delete records.
p.s. loving the Pro and Revised episodes Ryan!
But if you click a button_to so it opens in a new tab it will load a GET request. Tested in Chrome and Safari.
I added some extra logic to not add the delete members when destroy is not included. Supports both only and except route options.
This topic is interesting This topic is interesting. This information is very useful to me. I hope there will be many articles or like this article. Thank you for sharing.
Thansk for the share