5

Short version; jquery_ujs appears to conflict with Kaminari's AJAX support and I don't know why.

In my Rails 4 app, I have the following lines in my application.js

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .

If I remove jquery_ujs, then the following code stops working; it starts sending GET requests instead of DELETE requests and the user, per sending a GET request, simply receives the show page for the resource.

<%= link_to 'Delete Horse', horse, method: :delete, data: { confirm: 'Are you sure?' } %>

But if I leave jquery_ujs in, Kaminari w/ AJAX stops working....

<%= paginate @horses, :remote => true %>

goes nowhere when clicked (though the HREF tag in the rendered HTML is correct).

If I remove :remote => true from the paginate @horses link, then the link starts working. But, A) I'd like the AJAX to work for the sake of user experience, and B) I would like to understand why this is all happening.

NBarnes
  • 109
  • 6
  • 1
    Could you provide more detail on "goes nowhere"? Meaning no AJAX request is made because you can tell from the network tab in chrome inspector? Can you tell which javascript function is failing? – Ivan -Oats- Storck Dec 02 '13 at 00:12
  • 1
    So, what happens on the server? Is there a format .js method for index in HorsesController? – Ivan -Oats- Storck Dec 02 '13 at 00:22
  • Checking, it appears that clicking the link in the failing pagination case produces a request to the server that includes the following line, `Processing by HorsesController#index as JS`. In the case without the :remote => true, it calls `Processing by HorsesController#index as HTML` instead. The latter case works, the former does not. If I remove jquery_ujs from application.js, the failing case, with :remote => true, changes, in the request, from `as JS` to `as HTML`. So something about having jquery_ujs there is making the AJAX request change from `as HTML` to `as JS`, which then fails. – NBarnes Dec 02 '13 at 00:28
  • The controller#index doesn't have a respond_to block. The entire paginate method in the link declaration is Kaminari magic; I'm not sure what happens inside it. – NBarnes Dec 02 '13 at 00:30

1 Answers1

1

You will need to have your js.erb file to handle the request from Kaminari. In your js.erb, you should place the Javascript code to replace the current content with your new content from server.

So in your case, it sends the request as JS and that is the correct behavior. You can look at the reference here. http://jyothu-mannarkkad.blogspot.com/2013/06/rails-kaminari-ajax-pagination.html

The Lazy Log
  • 3,564
  • 2
  • 20
  • 27