Class: RageController::Inertia

Inherits:
API
  • Object
show all
Defined in:
lib/inertia/rage_controller/inertia.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inertia_share(**options) { ... } ⇒ Object

Shares data with all Inertia responses in a controller.

This method registers a before_action that evaluates the given block in the controller instance context and merges the returned hash into the shared data. The shared data is automatically included in all Inertia responses rendered by the controller.

Multiple inertia_share calls are cumulative - each block's data is merged into the existing shared data.

Examples:

Share data for all actions

class ApplicationController < RageController::Inertia
  inertia_share do
    { current_user: current_user&.as_json }
  end
end

Share data only for specific actions

class UsersController < ApplicationController
  inertia_share only: [:index, :show] do
    { permissions: current_user.permissions }
  end
end

Share data conditionally

class DashboardController < ApplicationController
  inertia_share if: :user_signed_in? do
    { notifications: current_user.unread_notifications }
  end
end

Parameters:

  • options (Hash)

    options passed to before_action (e.g., only:, except:, if:, unless:)

Yields:

  • Block evaluated in controller context that returns a Hash of data to share

Yield Returns:

  • (Hash)

    the data to merge into the shared props

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/inertia/rage_controller/inertia.rb', line 42

def inertia_share(**options, &block)
  raise ArgumentError, "inertia_share requires a block" unless block

  before_action(**options) do
    data = instance_eval(&block)
    return unless data

    if self.inertia_shared_data
      self.inertia_shared_data.merge!(data)
    else
      self.inertia_shared_data = data
    end
  end
end

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:



94
95
96
# File 'lib/inertia/rage_controller/inertia.rb', line 94

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



109
110
111
112
113
114
115
116
117
# File 'lib/inertia/rage_controller/inertia.rb', line 109

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



73
74
75
76
77
78
79
80
81
82
# File 'lib/inertia/rage_controller/inertia.rb', line 73

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