Module: Turbolinks::Redirection

Included in:
Controller
Defined in:
lib/turbolinks/redirection.rb

Overview

Provides a means of using Turbolinks to perform renders and redirects. The server will respond with a JavaScript call to Turbolinks.visit/replace().

Constant Summary collapse

MUTATION_MODES =
[:change, :append, :prepend].freeze

Instance Method Summary collapse

Instance Method Details

#redirect_to(url = {}, response_status = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/turbolinks/redirection.rb', line 7

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

  if turbolinks
    response.content_type = Mime[:js]
  end

  return_value = super(url, response_status)

  if turbolinks
    self.status = 200
    self.response_body = "Turbolinks.visit('#{location}'#{_turbolinks_js_options(options)});"
  end

  return_value
end


45
46
47
48
# File 'lib/turbolinks/redirection.rb', line 45

def redirect_via_turbolinks_to(url = {}, response_status = {})
  ActiveSupport::Deprecation.warn("`redirect_via_turbolinks_to` is deprecated and will be removed in Turbolinks 3.1. Use redirect_to(url, turbolinks: true) instead.")
  redirect_to(url, response_status.merge!(turbolinks: true))
end

#render(*args, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/turbolinks/redirection.rb', line 25

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]

    render_options = _normalize_render(*args, render_options, &block)
    body = render_to_body(render_options)

    self.status = 200
    self.response_body = "Turbolinks.replace('#{view_context.j(body)}'#{_turbolinks_js_options(options)});"
  else
    super(*args, render_options, &block)
  end

  self.response_body
end