Unable to get a simple live view working

Followed the following steps from programming-phoenix-liveview book
mix phx.new pento
mix phi.server works as expected

Then added the following to router.ex

scope "/", PentoWeb do
    pipe_through :browser

    get "/", PageController, :index
    live "/guess", WrongLive
  end

created file live/wrong_live.ex with the following content

defmodule PentoWeb.WrongLive do
  use Phoenix.LiveView, layout: {PentoWeb.LayoutView, "live.html"}

  def mount(_params, _session, socket) do
    {:ok, assign(socket, score: 0, message: "Make a guess:")}
  end

  def render(assigns) do
    ~H"""
      <h1>Your score: <%= @score %></h1>
      <h2>
        <%= @message %>
      </h2>
      <h2>
        <%= for n <- 1..10 do %>
          <a href="#" phx-click="guess" phx-value-number= {n} ><%= n %></a>
        <% end %>
    </h2>
    """
  end
end

Once I restart the server localhost:4000/guess is supposed to show the live view, but I get the Not found

Looks fine to me.

You could confirm with mix phx.routes which should list all the configured routes including a live_path GET /guess ... and perhaps confirm there are no compilation errors by restarting the server?

Is live/wrong_live.ex inside <pento-project>/lib/pento_web/live/wrong_live.ex (correct) or <pento-project>/live/wrong_live.ex (incorrect)? Code needs to be inside lib.

Or makes sure you’re not accidentally working on a different project folder by accident.

1 Like

I am attaching my screenshot, even the /dev/mailbox does not work


I don’t see anything wrong with it.

Your code is correct.

Maybe you deleted something.

Try

mix phx.new pento_web --no-ecto

and do only add this one line to the router:

and add your LV module.

And it will work with latest phx.

The reason it did not work is I did not add import Phoenix.LiveView.Router to the router.ex file at the top. Thanks for the help.

4 Likes