cro

cro

Programming Phoenix LiveView: P27, live_path confusion

I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into the live_patch function to patch the view and add the button. Many examples involving live_patch I found on the web suggested something like

<%= live_patch to: Routes.live_path(@socket, __MODULE__), replace: true do %>
   <button>Try again!</button>
<%end>

but Elixir complains with

warning: Routes.live_path/2 is undefined (module Routes is not available or is yet to be defined)
  lib/pento_web/live/wrong_live.ex:50: PentoWeb.WrongLive.render/1

mix phx.routes says the helper is defined:

$ mix phx.routes
          page_path  GET  /                                      Phoenix.LiveView.Plug :index
          live_path  GET  /guess                                 Phoenix.LiveView.Plug PentoWeb.WrongLive
live_dashboard_path  GET  /dashboard                             Phoenix.LiveView.Plug :home
live_dashboard_path  GET  /dashboard/:page                       Phoenix.LiveView.Plug :page
live_dashboard_path  GET  /dashboard/:node/:page                 Phoenix.LiveView.Plug :page
          websocket  WS   /live/websocket                        Phoenix.LiveView.Socket
           longpoll  GET  /live/longpoll                         Phoenix.LiveView.Socket
           longpoll  POST  /live/longpoll                         Phoenix.LiveView.Socket
          websocket  WS   /socket/websocket                      PentoWeb.UserSocket

So I must be missing a use or alias somewhere? Help?

Marked As Solved

SophieDeBenedetto

SophieDeBenedetto

Author of Programming Phoenix LiveView

Hello and thank you all for your patience! A lot has changed in LiveView lately and Bruce and I are hard at work on a new version of the book with the latest version and all new code samples. Keep an eye out for that in the next few weeks.

In the meantime, yes the live path helpers have been replaced with the link/1 function component Phoenix.Component — Phoenix LiveView v0.19.5.

I believe the latest version of the book that is currently out does reflect this change though. Please do make sure you’re working off of the latest version. If you’ve purchased the e-book, you’ll be emailed when a new Beta release is made available for download and you’ll have access to download it for free.

Also Liked

SophieDeBenedetto

SophieDeBenedetto

Author of Programming Phoenix LiveView

Re-posting here for visibility bc I accidentally replied privately:

Mystery solved!

At the top of your PentoWeb.WrongLive module, you should pull in the LiveView behavior like this:

defmodule PentoWeb.WrongLive do
  use PentoWeb, :live_view

I think there was someplace in the book where we gave you an outdated instruction to do so like this instead:

defmodule PentoWeb.WrongLive do
  use Phoenix.LiveView

That has since been corrected but I’m not sure if the fix is out in the Beta release from last week or if it will be included in the next one. In either case, that is the change you need to make and I’m sorry for any confusion this caused!

Let me know if this un-sticks you or if you encounter any other errors :slight_smile:
And @cro confirmed this fixed the issue!

SophieDeBenedetto

SophieDeBenedetto

Author of Programming Phoenix LiveView

Hi @IwateKyle, I’m sorry to hear that you found this particular exercise so challenging. Rest assured that live_patch is covered in greater detailed later on in the book and we will update the text to let readers know.

In the meantime, here is some guidance on the solution:

  • Per the docs here (and as shown in the original question on this thread above), you can write the live_patch function call in the markup in the render function of WrongLive like this:
<%= live_patch to: Routes.live_path(@socket, __MODULE__), replace: true do %>
      <button>Try again!</button>
<%end%>

This adds a button that is wrapped in a special kind of link called a “live patch”. A “live patch” link basically re-directs to the current live view without reloading it. From the docs, if you call live_patch with the route to the current live view…

…the new state is pushed to the client, without reloading the whole page while also maintaining the current scroll position

The value of the :to key in the argument to live_patch can be any route. To specify the route of the current live view, you use: Routes.live_path(@socket, __MODULE__) or you could use Routes.live_path(@socket, PentoWeb.WrongLive), which evaluates to the same thing. The third example in the docs here is accurate https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.Helpers.html#live_patch/2-examples.

To fully get this working, you need to add this live_patch link I described above, and you need to implement a new function in your live view, a handle_params/3 function. From the docs:

When navigating to the current LiveView, Phoenix.LiveView.handle_params/3 is immediately invoked to handle the change of params and URL state.

If you follow the link to the handle_params/3 docs, you will see that it takes in three arguments: some params, the URI and the socket, and it needs to return {:noreply, socket}. If we are aiming to “reset” the game, then we should return a socket with the appropriate starting state. So something like this:

def handle_params(_params, _uri, socket) do
    {
      :noreply,
      assign(
        socket,
        score: 0,
        message: "Guess a number."
      )
    }
  end

That should do it! I hope this helps and thanks very much for your feedback.

IwateKyle

IwateKyle

Bless you Sophie. You are a super star! I appreciate your time. I’ve since moved ahead, but I’ll definitely add this to exercise. :slight_smile:

Popular Prag Prog topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
kuroneko
Whilst the author has been careful to provide exact results for the tests elsewhere in the book (such as surds with the transformation te...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New

Latest in PragProg

View all threads ❯