Keeping the same path helpers when turning a view into a LiveView

You’ve been given the go-ahead to turn one of your existing views into a LiveView.

So you build a new UserView to be mounted on the "/users/:id" path.

There’s only one problem: routing to a live view with live/4 gives you a different path helper. 😱

# Original Path
resources "users", UserController, only: [:show]
# => user_path(conn, :show, user)

# New Path
live "/users/:id", UserLive
# => live_path(conn, UserLive, user)

What about the rest of your codebase that already uses user_path(conn, :show, user)? Do you have to change all those to live_path(conn, UserView, user)?

No. Thankfully, you don’t have to. It just takes some finagling with the options.

1) Use the as option to change the prefix from live_ to user_

live "/users/:id", UserLive, as: :user
# => user_path(conn, UserLive, user)

2) Give it the :show action to replace UserLive

live "/users/:id", UserLive, :show, as: :user
# => user_path(conn, :show, user)

Voilà! Celebrate all the changes you don’t have to make. 🎉