Phoenix Template Undefined looking for 'root.json'

calling: http://localhost:4000/m/7xfksimqi4dzrwmv I get the error:

 Phoenix.Template.UndefinedError at GET /m/7xfksimqi4dzrwmv
Could not render "root.json" for FaithfulWordWeb.LayoutView, please define a matching clause for render/2 or define a template at "lib/faithful_word_web/templates/layout/*". The following templates were compiled:

* _layout.html
* _layout.text
* _user_menu.html
* app.html
* email.html
* email.text
* live.html
* root.html
* session.html

my code:

  def show(conn, %{"readable_id" => readable_id}) do
    result = MediaItemApi.media_item_by_readable_id(readable_id)
    IO.inspect(result, label: "media_item_by_readable_id result")

    result
    |> case do
      {:error, nil} ->
        put_status(conn, 404)
        |> put_view(ErrorView)
        |> render("404.json", %{message: "error showing media item."})

      {:ok, media_item} ->
        render(conn, "show.html", media_item: media_item)
    end

I’m not sure why it doesn’t just render a 404? I just don’t get why phoenix is asking for a “root.json” template. The site is LiveView-centric, I’m just trying to render a 404 when the user doesn’t enter a correct readable_id on a mediaitem. Works fine when the correct readable_id is passed. I have this in my router:

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, {FaithfulWordWeb.LayoutView, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug FaithfulWordWeb.GenerateCSRF
    plug FaithfulWordWeb.AssignSession
    plug :fetch_current_user
  end

and the route:

  scope "/", FaithfulWordWeb do
    pipe_through :browser

    live "/", PageLive, :index
    get "/faq", FaqController, :new
    get "/m/:readable_id", ShareMediaItemController, :show
  end

I know it has something to do with plug :put_root_layout, {FaithfulWordWeb.LayoutView, :root} …

Fixed on master and v1.5 branches.

5 Likes

Got it, will update on next release.

Hi,

Have same issue, but despite a mix deps.update --all it is still failing with {:phoenix, “~> 1.5.9”}

Thanks

Seb

There hasn’t been a release yet, hopefully one is coming soon. You can use the v1.5 branch from git for now:

{:phoenix, "~> 1.5.9", github: "phoenixframework/phoenix", override: true}

Thanks Jose. This created an 1.6.0-dev conflict in mix file but adding the branch solves it

{:phoenix, "~> 1.5.9", github: "phoenixframework/phoenix", branch: "v1.5", override: true},

That’s the way to go indeed, thanks for catching it.

Is there a way to work around this without necessarily upgrading phoenix?

Phoenix v1.5.10 is out which addresses this!

3 Likes

Thanks, updated to 1.5.10 and the issue is resolved.

1 Like