I have run into an issue when using path helpers in a form_for function when trying to edit a resource. In this case I am trying to update an existing resource.
The router kicks out the following
categories_path GET /categories MyApp.CategoriesController :index
categories_path POST /categories MyApp.CategoriesController :create
categories_path PATCH /categories/:id MyApp.CategoriesController :update
PUT /categories/:id MyApp.CategoriesController :update
My form_for opens as
<%= form_for(@client_changeset, categories_path(@conn, :update, @client), fn f -> %>
(the categories controller handles submissions for both clients and topics as they are the âcategoriesâ)
My understanding is, providing a changeset for an existing resource to form_for means the submission would be a POST to
/categories/<id of resource>
with
â_methodâ => âputâ (or âpatchâ)?
I then take the params where I can use the id
for fetching the resource from storage then sending that along with the client
params for the update.
However. If I set up the form_for as shown I always get the error,
protocol Enumerable not implemented for %MyApp.Client{... etc }
If I remove the @client
from the call to categories_path
the error goes away, but the form submission does not include an id
in the path.
Revised form_for
<%= form_for(@client_changeset, categories_path(@conn, :update), fn f -> %>
POSTs to
/categories
As a workaround I have added the id @client.id
as a hidden field on the form so I can pluck it from the params. This however feels wrong and hacky, and doesnât align with the boilerplate code a phoenix generator produces or my understanding of the path_helper included in the form_for action.
Iâve checked the categories_path
in iex and I can pass
categories_path(MyApp.Endpoint, :update, Repo.get(Client, 1))
and it returns
"/categories/1"
as it should.
Any help here would be greatly appreciated. Thanks.