Is there a way of generating a path from the current controller, an action and any route parameters, without using a named route helper?
My use case is that I’m implementing a single controller on multiple paths, by passing in an assign
opt which changes the behaviour. I want index
in the controller to redirect to show
, without hardcoding the route, which will differ depending on the path the user is accessing the controller through.
For example:
get "/foo", MyController, :show, assigns: %{mode: App.Modes.Foo}
get "/foo/:id", MyController, :index, assigns: %{mode: App.Modes.Foo}
get "/bar", MyController, :show, assigns: %{mode: App.Modes.Bar}
get "/bar/:id", MyController, :index, assigns: %{mode: App.Modes.Bar}
defmodule AppWeb.MyController do
use AppWeb, :controller
def index(conn, _params) do
conn
|> redirect(to: path_to_show_action)
end
end