LiveView for each resource action when not using a modal

mix phx.gen.live generates LiveView, templates, and context for a resource. The generator creates two LiveViews, ResourceLive.Index and ResourceLive.Show.

ResourceLive.Index is also used for the new and edit actions. ResourceLive.Show is also used for edit. All of the forms (new & edit) rely on a modal to display to the user. The create and update actions are handled in the LiveView as well, but they don’t have routes in the router.

See generated router actions for my InjuredReserve resource below.

    live "/injured_reserves", InjuredReserveLive.Index, :index
    live "/injured_reserves/new", InjuredReserveLive.Index, :new
    live "/injured_reserves/:id/edit", InjuredReserveLive.Index, :edit

    live "/injured_reserves/:id", InjuredReserveLive.Show, :show
    live "/injured_reserves/:id/show/edit", InjuredReserveLive.Show, :edit

I don’t plan to use a modal in my app. Is the simplest approach to create a LiveView for both the new and edit actions (ResourceLive.New and ResourceLive.Edit) then handle the create and update actions in those LiveViews?

Any big downsides or gotchas to this approach?

I don’t think you’ll need to define actions if you have separate liveviews, but otherwise your approach looks fine.It’s a little dated, but this is the approach for creating/editing users in https://github.com/chrismccord/phoenix_live_view_example

1 Like