Skip to content

Instantly share code, notes, and snippets.

@douglas
Forked from savroff/inertiable.rb
Created June 5, 2024 13:19
Show Gist options
  • Save douglas/56f59aa9b8f51ec451bcd199108d40a3 to your computer and use it in GitHub Desktop.
Save douglas/56f59aa9b8f51ec451bcd199108d40a3 to your computer and use it in GitHub Desktop.
module Inertiable
extend ActiveSupport::Concern
included do
before_action :inertiable
end
def inertiable
response.set_header('Vary', 'Accept')
response.set_header('X-Inertia', true) if inertiable?
end
def inertiable?
request.headers['X-Inertia'].present?
end
def inertia_render(component, props = {})
props = @shared_props.merge(props) if @shared_props
component_name = component.to_s.camelize.gsub('::', '/')
data = {
component: component_name,
props: props,
url: request.url
}
if inertiable?
render json: data
else
html = helpers.tag.div(
id: 'app',
data: { page: data.to_json }
)
render html: html, layout: true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment