DEV Community


Posted on

Using Live Suffix in LiveView

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.

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
Enter fullscreen mode Exit fullscreen mode

And our files structure can be improved as well.

file organization

Top comments (1)

pierrelegall profile image
Pierre Le Gall

Interesting tip! 😎