Subscribe to The Rails Future        RSS Feed
-----

Rails - Sending delete signals to rails with jQuery since it broke PROTOTYPE's delete links

Icon 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...

Posted Image

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' %>



Posted Image

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 ]

There are no Trackbacks for this entry

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Tags

    Search My Blog

    1 user(s) viewing

    1 Guests
    0 member(s)
    0 anonymous member(s)

    Categories