Trouble with Redirects

So I’m having trouble trying to do a simple redirect from a LiveView event handler, and I’m not sure where my difficulty lies…

Firstly, is it possible to live_redirect to an entirely different LiveView? (This code is in CreateBoardLive.) I suppose this should be preferable, but it didn’t seem to work, nor did I get any error message.

    {:noreply, live_redirect(socket, to: Routes.live_path(socket, BoardLive, id))}

Then, I followed another example which seemingly should do a regular redirect.

    {:stop, redirect(socket, to: Routes.live_path(socket, BoardLive, id))}

In both cases, I can see my tuple is returning from handle_event/3, but nothing seems to happen on the web page – I’m still just looking at the form. No errors or interesting feedback is given in the browser or backend consoles.

For context, the code in question is here: https://github.com/djthread/lucidboard/blob/create-board-liveview/lib/lucidboard_web/live/create_board_live.ex#L70

Much thanks for any clarity on this!

Cheers.

2 Likes

I did notice this post, which seemed somewhat similar. I do see examples out there, though, so it seems to be functioning for others…

Make sure to update to the latest phoenix_live_view, there was briefly a bug in master that broke redirection. Also after you upgrade it’s always a good idea to cd assets && rm -rf node_modules && npm install just in case you’ve got old javascript laying around.

5 Likes

Thank you soo much for that post, @benwilson512. It seems that bug had bitten me, because when I followed your advice, I found both methods above work!

Very cool that I can live_redirect to an entirely disparate liveview.

Cheers! <3

3 Likes