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.

So how does Rails do it?

Rails uses a JS trick. You can also do the same thing using jQuery or
Prototype. Here is an example extracted from the http://merbclass.commaterial:

view:

<%= link_to "[delete]", resource(article, :delete), :class =>
"delete", :rel => resource(article) %>


The code above would usually send the user to the delete action which has a
confirmation form which calls the destroy action.
We can Ajaxify/enhance the process by adding the following snippet of JS:

jQuery(function($) {
  $("a.delete").click(function() {
    // jQuery idiom that allows use of 'this' inside the callback
    var 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;
  });
});


As you can see, we are using the rel attibute to get the url of the resource
and we are "post"ing a request with a fake method ("_method") set to
"delete". The controller returns a notice that we then display in the view.

If you want to learn more about this kind of advanced technics, you can join
us in the merb/rails3 training sessions: http://merbcamp.com ;)

I hope this helps.

- Matt



On Wed, Jan 7, 2009 at 8:56 PM, Roy Wright <[email protected]> wrote:

>
>
> On Jan 7, 2009, at 10:39 PM, [email protected] wrote:
>
> >
> > I seem to have run into an odd issue. I am trying to create a simple
> > link_to "delete", resource(@snippet), :method => 'delete' and it does
> > not seem to work. It renders:
> >
> > <a method="delete" href="/snippets/5">delete</a>
> >
> > I am wondering how I can make this happen without using the
> > delete_button helper...
>
> Try:
>
>   link_to('delete', resource(obj, :delete)
>
> Here's a good reference:
>
>   http://wiki.merbivore.com/development/resource_urls
>
> HTH,
> Roy
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"merb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/merb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to