开发者

Ajax callbacks not working with Rails 3.0.5 and jQuery 1.5.1

I've got ajax pagination working with the Kaminari gem on my website but I'm having difficulty getting ajax callbacks working

I'm using jquery-1.5.1, rails 3.0.5 and I have the latest rails.js file

My podcasts.html.haml looks as follows

#paginator
  = paginate @podcasts, :remote => true

#podcasts
  = render @podcasts

My index.js.erb file looks like this:

$('#podcasts').html('<%= escape_javascript render(@podcasts) %>');
$('#paginator').html('<%= escape_javascript(paginate(@podcasts, :remote => true).to_s) %>');

The pagination side of things works fine, and the pages do indeed load via ajax but I want to do some ajax callbacks and I just can't figure out how to get this working.

I've tried adding numerous variations of the following code to my application.js file but with no success at all:

$('#paginator a').bind('ajax:success', function(data, status, xhr) {alert("success!");})

I would expect the code above to fire off an alert once the ajax stuff has finished. Nothing is happening though.

Anyone got any ideas?

PS

The paginate method above, is from the Kaminari gem and it creates the following html:

<div id="paginator">
  <nav class="pagination">
    <a href="/podcasts" data-remote="true">Page 1</a>
    <a href="/podcasts?page=2" data-remote="true">Page 2</a>
    <a href="/podcasts?page=3" data-remote="true">Page 3</a>
  </na开发者_Go百科v>
</div>


I was not able to follow exactly your code, but I made a simple test (jquery 1.5.1, rails 3.0.5), and it works for me:

ajax_controller.rb

class AjaxController < ApplicationController
  def get_data
    respond_to do |format|
      format.js { sleep 1; render :json => "ajax value" }
    end
  end
end

index.html.erb

<%= link_to "get_data", get_data_path, :remote => true, :id => 'request' %>

<div id="response">
</div>

<%= javascript_tag do %>
    $("#response").html("default value");

    $("#request").bind("ajax:success", function(e, data, status, xhr) {
        $("#response").html(data);
    });
<% end %>

routes.rb

    get "ajax/index"
    get "ajax/get_data", :as => "get_data"

I hope this help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜