As mentioned in an earlier post, the recommended way is to offer a non JS
path using the delete action:
<%= 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 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;
});
});
If you prefer to not care about UJS you can simply do:
Let's imagine we have a list of articles and in the li of each article we
add the following link:
<%= link_to "[delete]", resource(article), :class => "delete" %>
Then just add the following 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("href"), {"_method": "delete"}, function() {
self.parents("li:first").remove();
$("div.notice").html("Article deleted");
});
}
return false;
});
});
The code is pretty simple and compact so let me know if it doesn't make
sense.
- Matt
On Thu, Jan 8, 2009 at 11:37 AM, Ezra Zygmuntowicz <[email protected]>wrote:
>
>
> On Jan 7, 2009, at 8: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...
>
>
> 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
> [email protected]
>
>
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---