Skip to content
This repository has been archived by the owner on Jul 28, 2018. It is now read-only.

Commit

Permalink
Fix 'header already sent' error on Rails 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibaut committed Oct 11, 2015
1 parent 9795c4b commit 6404ae1
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/turbolinks/redirection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,36 @@ module Redirection

def redirect_to(url = {}, response_status = {})
turbolinks, options = _extract_turbolinks_options!(response_status)
turbolinks = (request.xhr? && (options.size > 0 || !request.get?)) if turbolinks.nil?

value = super(url, response_status)
if turbolinks
response.content_type = Mime::JS
end

return_value = super(url, response_status)

if turbolinks || (turbolinks != false && request.xhr? && (options.size > 0 || !request.get?))
_perform_turbolinks_response "Turbolinks.visit('#{location}'#{_turbolinks_js_options(options)});"
if turbolinks
self.status = 200
self.response_body = "Turbolinks.visit('#{location}'#{_turbolinks_js_options(options)});"
end

value
return_value
end

def render(*args, &block)
render_options = args.extract_options!
turbolinks, options = _extract_turbolinks_options!(render_options)
turbolinks = (request.xhr? && options.size > 0) if turbolinks.nil?

if turbolinks
response.content_type = Mime::JS
end

super(*args, render_options, &block)

if turbolinks || (turbolinks != false && request.xhr? && options.size > 0)
_perform_turbolinks_response "Turbolinks.replace('#{view_context.j(response.body)}'#{_turbolinks_js_options(options)});"
if turbolinks
self.status = 200
self.response_body = "Turbolinks.replace('#{view_context.j(response.body)}'#{_turbolinks_js_options(options)});"
end

self.response_body
Expand All @@ -41,12 +53,6 @@ def _extract_turbolinks_options!(options)
[turbolinks, options]
end

def _perform_turbolinks_response(body)
self.status = 200
self.response_body = body
response.content_type = Mime::JS
end

def _turbolinks_js_options(options)
if options[:change]
", { change: ['#{Array(options[:change]).join("', '")}'] }"
Expand Down

0 comments on commit 6404ae1

Please sign in to comment.