adam

adam

Path Helpers and form_for

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.

Marked As Solved

NobbZ

NobbZ

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

Also Liked

amnu3387

amnu3387

It’s either gnomes or robots you never know

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.

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement