Skip to content

Conversation

skryukov
Copy link
Contributor

This PR improves inertia routes helper by adding support for:

  • namespaced and scoped routes
namespace :settings do
  inertia 'appearance' => 'settings/appearance'
end

scope :settings do
  inertia 'appearance' => 'settings/appearance'
end
  • symbol notation for route paths
inertia settings: 'settings'
  • shorthand syntax with default component:
inertia :settings # => pages/settings.jsx

namespace :settings do
  inertia :appearance # => pages/settings/appearance.jsx
end

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.

Looks great, thank you! Just a quick question to make sure I understand the code.

get(route => 'inertia_rails/static#static', defaults: { component: component })
def extract_route_and_component(args)
if args.is_a?(Hash)
args.first
Copy link
Collaborator

Choose a reason for hiding this comment

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

Now matter how much Ruby I write, I'm still regularly surprised by syntax 😄

@scope = @scope.new(module: nil)
get(route, to: 'inertia_rails/static#static', defaults: { component: component })
ensure
@scope = @scope.parent
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you explain what this is doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This overrides defaults coming from the namespace helper. Btw, I missed the resource helper, will update the PR to support it as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If I understand correctly, Rails will prepend module/namespace/resource paths onto route in get(route.... That information is stored in @scope. But we're already taking all that into account in extract_route_and_component. @scope.new(module: nil) just clears out the data that would prepend those paths, and the ensure block sets everything back for the next line in the routes file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a cleaner solution 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

super clean looking!

if args.is_a?(Hash)
args.first
elsif resource_scope?
[args, InertiaRails.configuration.component_path_resolver(path: [@scope[:module], @scope[:controller]].compact.join('/'), action: args)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a bit simplistic but I hope it's good enough:

resources :foo do
  resources :bar do
    inertia :baz # => pages/bar/baz.tsx
  end
end

Copy link
Contributor

@brodienguyen brodienguyen Mar 2, 2025

Choose a reason for hiding this comment

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

Did you mean foo instead of pages please?

resources :foo do
  resources :bar do
    inertia :baz # => foo/bar/baz.tsx
  end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, pages/bar/baz.tsx mimics Rails routing: https://guides.rubyonrails.org/routing.html#nested-resources

@bknoles
Copy link
Collaborator

bknoles commented Mar 3, 2025

This looks good to go to me! Here's how it looks in the routes file.

image

Brandon, do you still have questions or can we merge?

@skryukov skryukov force-pushed the fix-inertia-routes-helper branch from 30674cb to 376bc8e Compare March 4, 2025 14:26
@bknoles bknoles merged commit 057556d into inertiajs:master Mar 6, 2025
17 checks passed
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.

4 participants