Try:
link_to('delete', resource(obj, :delete)
Here's a good reference:
http://wiki.merbivore.com/development/resource_urls
HTH,
Roy
resource(@article, :delete) # GET => deleteA GET request to the delete action won't destroy the object, it should display a warning screen confirming that the user really wanted to destroy the object.
<%= link_to "[delete]", resource(article, :delete), :class => "delete", :rel => resource(article) %>
jQuery(function($) {$("a.delete").click(function() {// jQuery idiom that allows use of 'this' inside the callbackvar self = $(this);if(confirm("Are you sure you wish to delete this article")) {$.post($(this).attr("rel"), {"_method": "delete"}, function(json) {self.parents("li:first").remove();$("div.notice").html(json.notice);}, "json");}return false;});});
so is the preferred approach than to rename all destroy methods, to
delete methods?
> Roy, if you look at the example I put in the wiki:
> resource(@article, :delete) # GET => delete
>
>
> A GET request to the delete action won't destroy the object, it
> should display a warning screen confirming that the user really
> wanted to destroy the object.
>
> What justin is trying to do isn't possible in pure HTML. Let me
> explain quickly.
>
> A restful destroy action on a resource needs to be called using the
> DELETE http verb/method. Browsers don't support all the http verbs
> such as DELETE and UPDATE. Instead, merb cheats by doing a post and
> "stuff" the method to us in a parameter.
>
> The same way you cannot POST with a link, you can't DELETE with a
> link and that's why we have the delete_button helper which creates a
> form for you and send call the destroy action by using the DELETE
> http verb in a request made to the resource.
OK, I actually cheat a little in the controller and have the delete
call the destroy method. I really didn't want the "are you sure"
prompt, so this seems to work fine from a link_to('delete',
resource(obj, :delete))
def destroy
if get_resource.destroy
redirect resource(controller_name.to_sym)
else
raise InternalServerError
end
end
def delete(id)
destroy
end
Have fun,
Roy
Yeah, this is pretty evil. Not only is it wrong, in the sense that GET
requests aren't supposed to modify anything, it opens you up to some
pretty nasty user experience bugs.
You should listen to Matt. Seriously.
I think specifically the problem here (and the real reason this should
not be done) is that a crawler (using get requests) can actually
delete an object.
To me, this is the real danger and the real issue and the reason for
avoiding this type of web coding. Not because "it's evil", or "it will
make the web unhappy" or "Matt won't be my friend". ;-)
Jim
>
> You should listen to Matt. Seriously.
--
Jim Freeze
That's why it's evil and that's why it makes the web unhappy. They're
the same thing.
Crawler and "WebAccelerators" are evil!
Le 8 janv. 09 à 17:17, Michael D. Ivey a écrit :
--
Nicolas Cavigneaux
http://www.bounga.org
http://www.cavigneaux.net
However, before the final decision to use HTTP GET or POST, please also consider considerations for sensitive data and practical considerations.
- Matt
Here is how you can make it happen. You need this javascript in your
app:
$(document).ready(function(){
$('a[method]').livequery(function(){
var message = $(this).attr('confirm');
var method = $(this).attr('method');
if (!method && !message) return;
$(this).click(function(event){
if (message && !confirm(message)) {
event.preventDefault();
return;
}
if (method == 'post' || method == 'put' || method == 'delete') {
event.preventDefault();
var form = $("<form/>").attr('method', 'post').attr('action',
this.href).attr('style', 'display: none');
if (method != "post") {
form.append($('<input type="hidden" name="_method"/
>').attr('value', method));
}
form.insertAfter(this).submit();
}
});
});
)};
Then you can use a normal link_to like this:
= link_to "Destroy!", url(:decom, instance.id), :method
=> :delete, :confirm => "Are you sure? There is no undo."
Cheers-
-Ezra
Ezra Zygmuntowicz
e...@engineyard.com
<%= link_to "[delete]", resource(article, :delete), :class => "delete", :rel => resource(article) %>
jQuery(function($) {$("a.delete").click(function() {// jQuery idiom that allows use of 'this' inside the callbackvar self = $(this);if(confirm("Are you sure you wish to delete this article")) {$.post($(this).attr("rel"), {"_method": "delete"}, function(json) {self.parents("li:first").remove();$("div.notice").html(json.notice);}, "json");}
return false;});});
<%= link_to "[delete]", resource(article), :class => "delete" %>
jQuery(function($) {$("a.delete").click(function() {// jQuery idiom that allows use of 'this' inside the callbackvar self = $(this);if(confirm("Are you sure you wish to delete this article")) {
$.post($(this).attr("href"), {"_method": "delete"}, function() {self.parents("li:first").remove();$("div.notice").html("Article deleted");
});}
return false;});});
My point was that some people like to know the real cost. There are
numerous examples of people succeeding by going against accepted
convention. Just saying something is evil is a lame explanation. (Good
thing DHH didn't listen to people saying what he was doing was evil).
Saying 'it is evil' is no where near the same as saying 'your data
could be destroyed by accident'.
Jim
--
Jim Freeze
Great explanation Matt. I learned something too. "POST request should
always be redirected to a GET".
Thanks
Jim
--
Jim Freeze