DEV Community 👩‍💻👨‍💻

Camilo
Camilo

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

And our files structure can be improved as well.

file organization

Top comments (1)

Collapse
 
pierrelegall profile image
Pierre Le Gall

Interesting tip! 😎

Join us at DEV
Yes, this is technically an “ad”, but really we just want to ask if you want to join DEV. We have 900k+ developers reading, posting, and enjoying community, and would love to have you.   Create an account and continue your coding journey.