Form based on many to many relation and how to apply it

Hi, I am following instructions from a blog - Many to many associations in Elixir and Phoenix - DEV Community

I am trying to allow a user to choose products from a list using checkoboxes. I am getting this error - (CompileError) undefined function f/0 in my template. What am I doing wrong?

This is my template:

   <div class="form-group">
      <%= 
        multiselect_checkboxes(
          f,
          :foods,
          Enum.map(@foods, fn f -> {f.name, f.id} end),
          selected: Enum.map(@changeset.data.foods,&(&1.id))
        )
       %>
  </div>

This is my multiselect_checkboxes function

  def multiselect_checkboxes(form, field, options, opts \\ []) do
    {selected, _} = get_selected_values(form, field, opts)
    selected_as_strings = Enum.map(selected, &"#{&1}")

    for {value, key} <- options, into: [] do
      content_tag(:label, class: "checkbox-inline") do
        [
          tag(:input,
          name: input_name(form, field) <> "[]",
          id: input_id(form, field, key),
          type: "checkbox",
          value: key,
          checked: Enum.member?(selected_as_strings, "#{key}")
          ),
          value
        ]
      end
    end
  end

Hi @borybar, can you show more of your template? Do you have a form_for anywhere?

I do not have form_for anywhere. I guess I should add it before like this, yes?

 <%form_for @changeset, Routes.consumption_path(@conn, :create), fn f ->  %>
      <%= 
        multiselect_checkboxes(
          f,
          :foods,
          Enum.map(@foods, fn f -> {f.name, f.id} end),
          selected: Enum.map(@changeset.data.foods,&(&1.id))
        )
      %>
  <% end %>

You need a form_for. Something like you posted.
I’d advice to read the docs.