RESTful link_to :method => destroy in Rails 3, with fallback for users with javascript disabled
Is there a simple RESTful way to have a fallba开发者_开发问答ck url for the link_to
with :method => :delete
that will work for folks who have javascript disabled?
For example, suppose I have a list of comments. To add a link to delete a comment, I might put:
<%= link_to "Delete", comment_path(comment.id), :method => :delete %>
This would produce a link with the data-method="delete"
that my ujs driver (jquery_ujs in my case) will turn into a DELETE request. However, when javascript is turned off, the link is followed as a GET request (as advertised in the docs), and my controller gets rightfully confused.
Is there a good and simple way to resolve this problem? There's Railscast 77, which shows one solution, but this seems to require an awful lot of extra code and throws out the quite elegant :method => :delete
solution. I'd be OK with it producing a link to a confirm-delete page when javascript is disabled.
The only solution I can think of is to use button_to
on the page and UJS to replace it with the corresponding link_to
. This somehow doesn't seem quite right to me, but maybe it's OK? Any other ideas?
Personally i don't like the idea of mapping the :delete
to a :get
, it's not RESTFul...The solution i use is the same than the one you have been thinking about, ie a button_to
instead of the link_to
, and if needed i can use CSS to style the button so that it looks like a link.
精彩评论