Jun 16, 2012 | 8 minutes | Views, Controllers, Routing
- source codeProject Files in Zip (93.2 KB)
- mp4Full Size H.264 Video (18.6 MB)
- m4vSmaller H.264 Video (9.52 MB)
- webmFull Size VP8 Video (13 MB)
- ogvFull Size Theora Video (21 MB)
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/deletelooks 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.
And I needed to add a delete method to the application_controller:
After that it worked beautifully.
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
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