Skip to content

Conversation

ElMassimo
Copy link
Contributor

@ElMassimo ElMassimo commented Aug 5, 2022

Description 📖

This pull request allows to leverage the default layout resolution in Rails.

Background 📜

When using Rails as the backend for different Inertia apps, having a global layout configuration does not allow each app to have a different layout (with different title and meta tags, for example).

By using the layout specified by the controller (which defaults to application in most apps), the user has more flexibility to choose which layout should be used.


For example, this pull request enables:

class Admin::BaseController < ApplicationController
  layout "admin"
end

where the admin frontend app gets its own layout, with different scripts, meta tags, and title.

Notes ✏️

  • The previous mechanism to configure a layout is still supported:

    InertiaRails.configure { |config| config.layout = 'other' }
  • An internal inertia_layout controller instance method was added for the purpose of even more dynamic configurations, for users that need the flexibility. Now they can customize the behavior by defining a method in their controllers, without having to monkey-patch InertiaRails.

Considerations about Backwards Compatibility

This shouldn't be considered a breaking change, as the default layout for most Rails apps is application (hence, the tests didn't need to be updated), and in other setups, a user would already be configuring InertiaRails::Configuration#layout explicitly, which will continue to work.

@@ -38,6 +38,11 @@ def redirect_back(fallback_location:, allow_other_host: true, **options)

private

def inertia_layout
layout = ::InertiaRails.layout
layout.nil? ? true : layout
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does returning true do here?

Copy link
Contributor Author

@ElMassimo ElMassimo Aug 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an inline doc, it lets Rails know that the view should use a layout.

@BrandonShar BrandonShar requested a review from bknoles August 13, 2022 00:34
Copy link
Collaborator

@BrandonShar BrandonShar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@BrandonShar BrandonShar merged commit 32a2506 into inertiajs:master Aug 30, 2022
@ElMassimo ElMassimo deleted the feat/layouts branch August 30, 2022 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants