Rails - Sending delete signals to rails with jQuery since it broke PROTOTYPE's delete links
25 February 2012
Leave Comment
UPDATE: This link solves the problem too, although this is still a good read to better know jQuery. http://buddylindsey....-ruby-on-rails/
You know the default index pages... say, you have a /users page, and you used the rails generators to make everything for you? And when you click the 'Delete' link next to one of the entries, PROTOTYPE takes over and sends a delete REST signal to the rails site? That's because browsers are dumb and they don't support the 'DELETE' action natively and so a piece of javascript needs to take over. In my opinion it's embarissingly foolish for rails to not have their own, SMALL, js library that is unobtrusive and allows you to add what ever javascript libraries you want ontop of that SMALL js file, but that's not how things work...

In rails, if you include jQuery (which is decidedly incompatible with PROTOTYPE) you will be breaking all of your index pages (at least as far as the delete link is concerned). There *seem* to be two ways arround this (comment if you have further details). There is a gem that you can install which will rewrite the helper methods to work with jQuery instead of PROTOTYPE.
Gem: https://github.com/rails/jquery-ujs (untested)
Alternatively, you can do what this guy did and hijack links and give them delete functionality:
ref: http://travisonrails...sts-with-jquery
::Instructions::
1) jQuery should already be included in your layouts/application.html.erb (that's why you're hear, right, jQuery broke everything?)
2) Put this at the top of your blah.html.erb file (try an index file to start out).
As the reference indicates, that snip of code will hijack all link's who's class is 'remote-delete'.
3) Exchange the rails' default delete link with this line of code.

When that's done, your link will now delete the user specified in it's href! For more details check out that reference, the guy did a bang up job!
ref: http://travisonrails...sts-with-jquery
Oh, here's a more advanced piece of code you can used instead of the more basic one I listed above. This one works with confirmations!
You know the default index pages... say, you have a /users page, and you used the rails generators to make everything for you? And when you click the 'Delete' link next to one of the entries, PROTOTYPE takes over and sends a delete REST signal to the rails site? That's because browsers are dumb and they don't support the 'DELETE' action natively and so a piece of javascript needs to take over. In my opinion it's embarissingly foolish for rails to not have their own, SMALL, js library that is unobtrusive and allows you to add what ever javascript libraries you want ontop of that SMALL js file, but that's not how things work...

In rails, if you include jQuery (which is decidedly incompatible with PROTOTYPE) you will be breaking all of your index pages (at least as far as the delete link is concerned). There *seem* to be two ways arround this (comment if you have further details). There is a gem that you can install which will rewrite the helper methods to work with jQuery instead of PROTOTYPE.
Gem: https://github.com/rails/jquery-ujs (untested)
Alternatively, you can do what this guy did and hijack links and give them delete functionality:
ref: http://travisonrails...sts-with-jquery
::Instructions::
1) jQuery should already be included in your layouts/application.html.erb (that's why you're hear, right, jQuery broke everything?)
2) Put this at the top of your blah.html.erb file (try an index file to start out).
<script>
$(document).ready(function() {
$('a.remote-delete').click(function() {
// we just need to add the key/value pair for the DELETE method
// as the second argument to the JQuery $.post() call
$.post(this.href, { _method: 'delete' }, null, "script");
window.setTimeout('location.reload()', 2); // refresh the page in 2 milliseconds
return false;
});
});
</script>
As the reference indicates, that snip of code will hijack all link's who's class is 'remote-delete'.
3) Exchange the rails' default delete link with this line of code.
<%= link_to 'Delete', entry_path(entry), :class => 'remote-delete' %> OR PERHAPS... <%= link_to 'Delete', user, :class => 'remote-delete' %>

When that's done, your link will now delete the user specified in it's href! For more details check out that reference, the guy did a bang up job!
ref: http://travisonrails...sts-with-jquery
Oh, here's a more advanced piece of code you can used instead of the more basic one I listed above. This one works with confirmations!
<script>
// This code will hijack the rails output of, say...
// link_to 'Delete', user, :class => 'remote-delete', :confirm => "Oh rly?"
// It seems to work fine without the confirm parameter
$(document).ready(function() {
$('a.remote-delete').click(function() {
var conf = $(this).attr('data-confirm');
if (conf != undefined){
var certain = confirm(conf);
}
if (certain || conf == undefined)
{
// we just need to add the key/value pair for the DELETE method
// as the second argument to the JQuery $.post() call
$.post(this.href, { _method: 'delete' }, null, "script");
window.setTimeout('location.reload()', 2); // refresh the page in 2 milliseconds
return false;
}
else{
return false;
}
});
});
</script>
0 Comments On This Entry
Trackbacks for this entry [ Trackback URL ]
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
My Blog Links
Recent Entries
-
-
Rails - Sending delete signals to rails with jQuery since it broke PROTOTYPE's delete linkson Feb 25 2012 08:56 AM
-
-
-
Web Related - Most Potent Librarieson Sep 06 2011 02:32 PM
Recent Comments
-
NotarySojac on Aug 03 2011 02:50 PM
005 Rails: From Views to Models -- The flow of data from the user
Search My Blog
1 user(s) viewing
1 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)












|