0

When calling pjax in Chrome (version 21) javascript console, I see two requests in my development log. I tried changing the timeout as was suggested in this post, but I still get two requests. Any ideas?

$.pjax({timeout: 4000,
      container: '[data-pjax-container]',
      url: document.location.href });
Community
  • 1
  • 1
GreenEggs
  • 527
  • 6
  • 17

1 Answers1

0

So the problem was that I wasn't rendering the application template, which returned nothing in the http body. Consequently, the pjax code it caused the request to fallback to a standard GET.

Why wasn't I rendering the application template? Well, I'm a dummy and was using this method in my application controller

def get_layout
  request.xhr? || request.headers['X-PJAX'] ? nil : 'application'
end

The problem here is that I'm using content_for in my index.html.haml.

- content_for :sub_nav do
 # view code
- content_for :main do
 # view code

My application.html.haml template looked like this...

.col1
  = yield :sub_nav
.col2
  = yield :main

Here's the solution...

I refactored the controller method into a application helper

  def is_pjax?
    request.xhr? || request.headers['X-PJAX'] ? true : false
  end

Then I modified the application view to not render various elements like header, footer, javascripts, css, etc

= render :partial => '/shared/footer' unless is_pjax?

Now the .pjax requests will render the page and template (left column, main, etc) BUT not the existing elements which are already loaded.

Hope this helps someone else.

GreenEggs
  • 527
  • 6
  • 17