Please read for an updated status on RailsCasts:
Learn more or hide this
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.
def resources(*args, &block)
yield if block_given?
delete :delete, action: :destroy
root to: 'products#index'
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.
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.
First sign in through GitHub to post a comment.