Skip to content

Instantly share code, notes, and snippets.

@savroff
Created August 15, 2020 00:11
Show Gist options
  • Save savroff/5837bb1488944847721f5e10e87c9592 to your computer and use it in GitHub Desktop.
Save savroff/5837bb1488944847721f5e10e87c9592 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