johmue
Two questions in short version
-
The
:routeroption oflive_render/3has disappeared from the docs in 0.17.6. In 0.17.10 it still seems to works in the unit tests. In the browser I still get a:not_mounted_at_routerissue. How can I passparamstomount/3when usinglive_render/3? -
How can I pass
paramstomount/3fromlive/2fromLiveViewTest?
Long version
I want to handle requests like /item/<id>?foo=bar in a way that %{"foo" => "bar"} is put to the session like put_session(conn, "foo", "bar") and id is to be passed to a LiveView’s mount/3 callback in the params argument.
That works following the hint from this article and using the :router option of live_render/3.
So I route the GET /item/<id> requests to a function
def show(conn, %{"foo" => bar} = _params) do
conn
|> put_session(conn, "foo", bar)
|> live_render(conn, MyLiveView, router: MyAppWeb.Router)
end
This works with phoenix_live_view 0.17.10 even though the :router option of live_render/3 has disappeared from the docs. So the question if it will work in the future and if not how to do it then.
Second question is how to call this LiveView from a test. If the LiveView is not mounted in the Router like live "/item/:id/", MyLiveView the call live(conn, "/item/1234") does not pass the id to MyLiveView’s mount/3.
What are my options? Maybe a completely different approach?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
andreaseriksson
I have another approach. For me, there is a scenarion when people leave the site and go to Stripe. So, I want to store the return to - path in a session so I can redirect them correct.
I basically use a JS-hook for this.
In the template;
Note that I encode the param so its not visible for the user:
ANd then a hook:
And that posts (actually GET) to a normal controller that decodes the path and add it to the session.
But, to be fair, I havent looked into if there is a better way to do this
johmue
As ever so often. A few minutes after posting the question I found the natural solution for my use case. So just for reference to future readers, this is how I finally solved it:
The endpoint
/item/<id>is routed directly into theLiveView, so detour through theshowfunction withlive_render.The
fooparameter is caught by aPlugthat sits at the end of thebrowserpipeline. The code of thePlugmodule looks like this:So if there is a
?foo=barin the request, it is put to the session and assigned to theassignsof theconn. If not thefooparameter is either fetched from the session or set to default.That is doing all I need.