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
There’s only one problem: routing to a live view with
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)? 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 "/users/:id", UserLive, as: :user # => user_path(conn, UserLive, user)
2) Give it the
:show action to replace
live "/users/:id", UserLive, :show, as: :user # => user_path(conn, :show, user)
Voilà! Celebrate all the changes you don’t have to make. 🎉