When using LiveView
and you don't include Live
inside your module name an error like this can appear:
Example: defmodule RankmodeWeb.Profiles.Index do
, will throw Live Suffix Error.
For example in my Rankmode Live Profiles module
If we change the module to RankmodeWeb.ProfilesLive.Index
it will run. But there is a secret option that will be valid too RankmodeWeb.Live.Profiles.Index
By using Live
as a Phoenix Context we can improve our code organization and avoid having to type Live
at the end of the module name.
We can see how the router.ex
is improved.
scope "/", RankmodeWeb do
pipe_through [:browser, :require_authenticated_user]
live "/profiles", Live.Profiles.Index, :index
live "/profiles/new", Live.Profiles.New, :new
live "/profiles/edit/:profile", Live.Profiles.Edit, :edit
live "/profiles/:profile/gameplays", Live.Gameplays.Index, :index
live "/profiles/:profile/gameplays/new", Live.Gameplays.New, :new
live "/profiles/:profile/gameplays/edit/:gameplay", Live.Gameplays.Edit, :edit
end
And our files structure can be improved as well.
Top comments (1)
Interesting tip! 😎