I wonder what is the recommended way to work with links in slice templates. Let’s say I have an Admin
slice that is mounted like this:
slice :admin, at: "/admin"
It has a very simple routing itself:
module Admin
class Routes < Hanami::Routes
get "/pages", to: "pages.index"
get "/page/:id", to: "pages.show", as: :page
end
end
Now on the pages.index
template I want to render a collection of links to pages. When I use slice routing, it produces links without the prefix:
> Admin::Slice["routes"].path(:page, id: 1)
=> "/page/1"
This makes sense, because why would a slice need to know how it’s mounted. Although maybe it could for practical reasons?
Using “global” routing I can do:
> Hanami.app["routes"].path(:admin_page, id: 1)
=> "/admin/page/1"
This is, indeed, what I want, but it turns out that the route name is not based on the slice name, but actually on the prefix on which the slice is mounted. If I change to this:
slice :admin, at: "/admin_panel"
… then :admin_page
no longer works. It is now :admin_panel_page
.
Is there a way to make links in slices really independent from how they are mounted, but also correct? For context, this would be extremely useful in slices distributed as gems, as in that case the slice really does not know where it would be mounted.
But maybe I missed something simple and there is a way to do that already?