Rails newbe here. I've got links that do ajax query to server:
<%= link_to g.name, g, :class => 'link link-to-gallery', :remote => true %>
JS view that loads data from partial:
$('#content').html('<%= escape_javascript render("layouts/galleries/show") %>');
And controller that operates with models and renders html and js:
class GalleriesController < ApplicationController def show @galleries = Gallery.all @gallery_to_show = Gallery.find(params[:id]) respond_to do |format| format.html format.js end end end
Ajax works fine, but I try to put callback after ajax completed:
jQuery(function() { $(".link-to-gallery").bind("ajax:complete", function() { alert(1) }); });
and alert never shows. What am I doing wrong? Rails version is 3.2.11, Ruby version is 1.9.3p194
Update: When I'm using this code
jQuery(function() { $(".link-to-gallery") .on("ajax:before", function () { alert("before") }) .on("ajax:complete", function() { alert("complete") }); });
"before" alert fires but only once. If I press again I gain nothing. But ajax still works.
on
, the preferred mechanism in jQuery 1.7+? – Dave Newtonon
instead ofbind
of jquery to handle events. – bullfrog