Module: Inertia::ControllerHelpers

Defined in:
lib/inertia/controller_helpers.rb

Overview

Provides controller helper methods for Inertia.js integration.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#redirect_back(fallback_location:, external: false) ⇒ Object

Redirects the client back to the referring page, with a fallback location.

Examples:

Redirect back with a fallback

redirect_back fallback_location: "/dashboard"

Parameters:

  • fallback_location (String)

    the URL to redirect to if there is no referer

  • external (Boolean) (defaults to: false)

    whether to force an external (full page) redirect. When true, the browser will perform a full page visit instead of an Inertia visit

See Also:



92
93
94
# File 'lib/inertia/controller_helpers.rb', line 92

def redirect_back(fallback_location:, external: false)
  redirect_back_or_to fallback_location, external:
end

#redirect_back_or_to(fallback_location, external: false) ⇒ Object

Redirects the client back to the referring page, or to the specified fallback location.

Examples:

Redirect back or to a fallback

redirect_back_or_to "/dashboard"

Force an external redirect

redirect_back_or_to "/dashboard", external: true

Parameters:

  • fallback_location (String)

    the URL to redirect to if there is no referer

  • external (Boolean) (defaults to: false)

    whether to force an external (full page) redirect. When true, the browser will perform a full page visit instead of an Inertia visit



107
108
109
110
111
112
113
114
115
# File 'lib/inertia/controller_helpers.rb', line 107

def redirect_back_or_to(fallback_location, external: false)
  referer = request.env["HTTP_REFERER"]

  if referer
    redirect_to referer, external:
  else
    redirect_to fallback_location, external:
  end
end

#redirect_to(location, external: false) ⇒ Object

Redirects the client to the specified location.

Examples:

Basic redirect

redirect_to "/dashboard"

Force an external redirect

redirect_to "https://example.com", external: true

Parameters:

  • location (String)

    the URL to redirect to

  • external (Boolean) (defaults to: false)

    whether to force an external (full page) redirect. When true, the browser will perform a full page visit instead of an Inertia visit



71
72
73
74
75
76
77
78
79
80
# File 'lib/inertia/controller_helpers.rb', line 71

def redirect_to(location, external: false)
  if external
    headers["x-inertia-location"] = location
    head 409
    return
  end

  head(request.get? || request.post? ? 302 : 303)
  headers["location"] = location
end