Assign @tags not available after validation failing

A select field fed with values from resource Tag

def new(conn, _params) do
  changeset = Projects.change_angebot(%Angebot{})
  tags = Projects.list_tags()

  render(conn, "new.html", changeset: changeset, tags: tags)
end

<%= select(f, :tag_id, Enum.map(@tags, &{&1.name, &1.id}), prompt: gettext("Select a tag")) %>

works great but if validation fails (phx goes to edit…) I get an error => assign @tags not available
ok. Try to give in edit

angebot = Projects.get_angebot!(id)
tags = Projects.list_tags()
changeset = Projects.change_angebot(angebot)
render(conn, "edit.html", changeset: changeset, angebot: angebot, tags: tags)

But no success…
Anything I missed?

Thanks for supporting.

Edit is only for existing data, are you sure you are in edit? From what you describe it does sound as if you were stuck in the create action, can you show it’s code?

You are right… I got irritated by the url: http://localhost:4000/edit/deine_projekte . (name edit… should be changed…)

def create(conn, %{"angebot" => angebot_params}) do
current_user = conn.assigns.current_user
angebot_params = Map.put(angebot_params, "user_id", current_user.id)

case Projects.create_angebot(angebot_params) do

  {:ok, angebot} ->
    conn
    |> put_flash(:info, "Angebot created successfully.")
    |> redirect(to: edit_angebot_path(conn, :show, angebot))
  {:error, %Ecto.Changeset{} = changeset} ->
    render(conn, "new.html", changeset: changeset)
end

end

You’re rendering the template with just a changeset supplied. You need to pass angebot and tags here as well.

Then your routing is non standard. You should probably rethink your routing strategy and adhere to phoenix default routing for resources.

works fine, on new/create and edit , but not on new when validation fails…
error => assign @tags not available in eex template

def new(conn, _params) do
  changeset = Projects.change_angebot(%Angebot{})
  tags = Projects.list_tags()

  render(conn, "new.html", changeset: changeset, tags: tags)
end

def create(conn, %{"angebot" => angebot_params}) do
  current_user = conn.assigns.current_user
  angebot_params = Map.put(angebot_params, "user_id", current_user.id)

case Projects.create_angebot(angebot_params) do

  {:ok, angebot} ->
    conn
    |> put_flash(:info, "Angebot created successfully.")
    |> redirect(to: edit_angebot_path(conn, :show, angebot))
  {:error, %Ecto.Changeset{} = changeset} ->
    render(conn, "new.html", changeset: changeset)
end
end

This needs to get changed.

routing is ok … just a silly naming…

No, usually there should only be a post to the resources name on create, no edit, no new, no anything.

lib/anders/edit

‘edit’ in my case is not a http method…
Did I get something wrong?

That looks like if it were your folder structure, that’s even just another thing…

I’m currently a bit of, about how your project is organized. Maybe this should better discussed somewhere else?

If your code is public, you could ask for a code review in the proper section of the forum. And since it seems to become some larger project you should ask early and learn from the early feedback to properly organize future code additions. You might even get contributors by this.

PS: also you really shouldn’t mix languages in the code. stick to either English or German, but do not use both in code and routes. Of course you can properly localise your application in any language.

=> mix languages… yes that is sloppy…
when I have my basics working in my first phx project I will rewrite it…
thanks so far…
remains the assign @tags not available point

For which a solution has been pointed out already 2 times:

ok. found it…thanks