adam

adam

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.

Showing Posts 1 to 9

idi527

idi527

What error do you get if you try

<%= form_for(@client_changeset, categories_path(@conn, :update, @client, []), fn f -> %>

?

Can you show the code you use to generate the routes for categories?

adam

adam OP

I get

undefined function categories_path/4

router.ex for categories reads

resources("/categories", CategoriesController, only: [:index, :create, :update])
NobbZ

NobbZ

Just to be sure the parser isn’t irritated, have you tried form for without parentheses?

amnu3387

amnu3387

It should be categories_path(@conn, :update, @client.id), it won’t extract the ID for you

idi527

idi527

Since categories_path/4 doesn’t exist, the third argument in categories_path/3 is an option list, so I doubt categories_path(@conn, :update, @client.id) would work since @client.id is not a list of options.

And there is usually a Phoenix.Param implementation or something like that to, in this case, extract id out of @client, so if categories_path(@conn, :update, @client.id) worked, categories_path(@conn, :update, @client) would work as well.

amnu3387

amnu3387

nevermind - now it works for me without id too, must be the gnomes

adam

adam OP

Nobbz,

I appreciate you answering. Total face-palm moment.

The page has two forms on it, for the two categories. I was (like a complete fool) reading the error message which was about the second form while making changes to the first one!

Adding and removing parentheses finally woke me up to this as the stacktrace showed parens still in place. THAT’S WHEN I REALIZED. :frowning:

Sorry to have wasted everyone’s time…

adam

adam OP

See my note about me being a total fool, but, just FYI it works for me just using client, I don’t need to add the .id

Now I need to work on my stack trace reading skills.

amnu3387

amnu3387

It’s either gnomes or robots you never know

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews