Skip to content

Instantly share code, notes, and snippets.

@edison
Created May 19, 2011 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edison/980351 to your computer and use it in GitHub Desktop.
Save edison/980351 to your computer and use it in GitHub Desktop.
Pjax with ruby 1.8.7
module Pjax
extend ActiveSupport::Concern
included do
layout lambda {|c| c.pjax_request? ? false : 'application' }
end
private
def redirect_pjax_to(action, url = nil)
new_url = url_for(url ? url : { :action => action })
render :js, <<-EJS
if (!window.history || !window.history.pushState) {
window.location.href = '#{new_url}';
} else {
$('div.pages').html(#{render_to_string("#{action}.html.erb").to_json});
$(document).trigger('end.pjax');
var title = $.trim($('div.pages').find('title').remove().text());
if (title) document.title = title;
window.history.pushState({}, document.title, '#{new_url}');
}
EJS
end
def pjax_request?
env['HTTP_X_PJAX'].present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment