I have a form that allows you to create and edit a Student with multiple Skills. It uses a multiple_select and looks like this: <%= multiple_select f, :skills, @skills, prompt: "Choose skills", selected: Enum.map(@student.skills, &(&1.id)) %>
This works perfectly fine when you are Editing a student, but it doesn’t work when you are Creating a New Student. The error it gives me is that student doesn’t exist in the template, which makes sense because I haven’t created a student yet.
What is the proper way to handle this? The documentation doesn’t seem to cover this. Should there be multiple forms or should I be doing some kind of conditional in the multiple_select?
This makes sense. I should have more thoroughly examined the changeset. I’m very new to Elixir and Phoenix and just didn’t expect it to work correctly for some reason.
I had actually worked out a temporary workaround by doing some work in the View that looked like this.
def get_selected_skills(conn) do
if Map.has_key?(conn.assigns, :student) do
Enum.map(conn.assigns.student.skills, &(&1.id))
else
[]
end
end
Your method obviously makes a lot more sense. Thanks again for the help!