Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not seem possible to use assign in custom error html #5726

Open
srcrip opened this issue Feb 15, 2024 · 4 comments
Open

Does not seem possible to use assign in custom error html #5726

srcrip opened this issue Feb 15, 2024 · 4 comments
Assignees

Comments

@srcrip
Copy link
Contributor

srcrip commented Feb 15, 2024

Environment

  • Elixir version (elixir -v): 15
  • Phoenix version (mix deps): main
  • Operating system: mac

Actual behavior

If you try to use assign in a custom error html like this:

defmodule ExampleWeb.ErrorHTML do
  use ExampleWeb, :html

  def render("404.html" = template, assigns) do
    assigns =
      assign(assigns,
        message: hello
      )

    ~H"""
    <h1 class="text-2xl font-bold tracking-tight text-gray-900 sm:text-4xl">
      <%= @status %> <%= @message %>
    </h1>
    """
  end
end

You get an error like:

(ArgumentError) assign/3 expects a socket from Phoenix.LiveView/Phoenix.LiveComponent  or an assigns map from Phoenix.Component as first argument ...

Expected behavior

This should be a valid assigns so I think it should work?

@srcrip srcrip closed this as completed Feb 15, 2024
@srcrip srcrip reopened this Feb 15, 2024
@agonzalezro
Copy link
Contributor

agonzalezro commented Feb 18, 2024

@srcrip assign expects the socket as the first argument. I am not sure if you would be able to get the socket in the custom errors, but for this specific example you are mentioning maybe it's enough with:

defmodule ExampleWeb.ErrorHTML do
  use ExampleWeb, :html

  def render("404.html" = template, assigns) do
    message = "hello"

    ~H"""
    <h1 class="text-2xl font-bold tracking-tight text-gray-900 sm:text-4xl">
      <%= @status %> <%= message %>
    </h1>
    """
  end
end

I know that you pasted just an example so maybe it doesn't fit what you need. But let me know.

@josevalim
Copy link
Member

The error message is correct. This is not the render function of a LiveView nor of a component. We could relax it here and allow assign to work with any map, but there is no change tracking happening here whatsoever, so I am unsure if we should do it. It is up to @chrismccord to decide if he wants to allow assigns to be used anywhere, even outside of the regular rendering stack.

@thiagomajesk
Copy link

thiagomajesk commented Apr 20, 2024

Not directly related to the issue, but check this comment.

I guess this should be addressed once the component model is consolidated... IMHO, there's still some mental friction between how things used to be before (views/templates) vs after (components/templates) Phoenix 1.7.

Since functional components were introduced, people expect functions that return HTML using ~H to behave similarly, and we have some cases where this is handled slightly differently by the framework (like layouts for instance).

@josevalim
Copy link
Member

We should set __changed__: nil before calling the error view.

@chrismccord chrismccord self-assigned this May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants