-
-
Save douglas/56f59aa9b8f51ec451bcd199108d40a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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