Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

gem does not distinguish between nested routes with different params #42

Open
2 tasks done
reinvanimschoot opened this issue Dec 29, 2023 · 2 comments
Open
2 tasks done
Labels
bug: pending triage Something doesn't seem to be working, but hasn't been verified

Comments

@reinvanimschoot
Copy link

reinvanimschoot commented Dec 29, 2023

  • I have tried upgrading by running bundle update js_from_routes.
  • I have read the troubleshooting section before opening an issue.

Description 馃摉

In my routes.rb I have the following setup:

resources :sections, path: "s", param: :slug do
  resources :discussions
end

resources :custom_sections, path: "cs", param: :slug do
  resources :discussions
end

This results in only one set of paths in the DiscussionsApi.ts:

// DO NOT MODIFY: This file was automatically generated by JsFromRoutes.
import { definePathHelper } from '@js-from-routes/client'

export default {
  index: /* #__PURE__ */ definePathHelper('get', '/s/:section_slug/discussions'),
  create: /* #__PURE__ */ definePathHelper('post', '/s/:section_slug/discussions'),
  new: /* #__PURE__ */ definePathHelper('get', '/s/:section_slug/discussions/new'),
  edit: /* #__PURE__ */ definePathHelper('get', '/s/:section_slug/discussions/:id/edit'),
  show: /* #__PURE__ */ definePathHelper('get', '/s/:section_slug/discussions/:id'),
  update: /* #__PURE__ */ definePathHelper('patch', '/s/:section_slug/discussions/:id'),
  destroy: /* #__PURE__ */ definePathHelper('delete', '/s/:section_slug/discussions/:id'),
}

So there is no way for me to distinguish between section_discussion_path and custom_section_discussion_path, since using DiscussionsApi.show.path({custom_section_slug}) would result in an error.

Is there a way around this?

Thanks!

@reinvanimschoot reinvanimschoot added the bug: pending triage Something doesn't seem to be working, but hasn't been verified label Dec 29, 2023
@ElMassimo
Copy link
Owner

Hi Rein!

This gem currently assumes each controller action has a single path, allowing it to group helpers by controller name.

A workaround to disambiguate is to have two stub controllers, SectionDiscussionsController < DiscussionsController and CustomSectionDiscussionsController < DiscussionsController:

resources :sections, path: "s", param: :slug do
  resources :discussions, controller: :section_discussions
end

resources :custom_sections, path: "cs", param: :slug do
  resources :discussions, controller: :custom_section_discussions
end

which would then generate sectionDiscussions and customSectionDiscussions path helpers.

If I remember correctly, the rails route objects don't contain enough information about the "parent scopes" as to do this automatically in the gem.

@reinvanimschoot
Copy link
Author

@ElMassimo Thanks for the reply and the workaround!

Regarding the information being available or not, something like js-routes does seem to manage to create a helper called customSectionsDiscussionsPath. So I guess the information is indeed available? I could be wrong though!

The workaround is helpful for now but having something like Section::ImagesController generate an ImagesApi does seem difficult to manage if the only solution is creating stub controllers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug: pending triage Something doesn't seem to be working, but hasn't been verified
Projects
None yet
Development

No branches or pull requests

2 participants