diff --git a/lib/turbolinks.rb b/lib/turbolinks.rb index ceddab82..aad12426 100644 --- a/lib/turbolinks.rb +++ b/lib/turbolinks.rb @@ -45,7 +45,11 @@ def call_with_turbolinks(env) ActiveSupport.on_load(:action_view) do (ActionView::RoutingUrlFor rescue ActionView::Helpers::UrlHelper).module_eval do - include XHRUrlFor + if defined?(prepend) && Rails.version >= '4' + prepend XHRUrlFor + else + include LegacyXHRUrlFor + end end end end diff --git a/lib/turbolinks/xhr_url_for.rb b/lib/turbolinks/xhr_url_for.rb index 0a1c14fb..e292eeae 100644 --- a/lib/turbolinks/xhr_url_for.rb +++ b/lib/turbolinks/xhr_url_for.rb @@ -3,6 +3,14 @@ module Turbolinks # option by using the X-XHR-Referer request header instead of the standard Referer # request header. module XHRUrlFor + def url_for(options = {}) + options = (controller.request.headers["X-XHR-Referer"] || options) if options == :back + super + end + end + + # TODO: Remove me when support for Ruby < 2 && Rails < 4 is dropped + module LegacyXHRUrlFor def self.included(base) base.alias_method_chain :url_for, :xhr_referer end