wolfiton

wolfiton

Select element in a nested form

Hi everyone,

I am getting an error when i try to add a select in a nested form

The error no function clause matching in Keyword.pop/3

My select code:

<div class="field">
  <%= label f, :category, class: "label" %>
  <div class="control">

    <%= select f, :category_id, @categories, fn cf -> %>
    IO.inspect(@categories)
  </div>
  <p class="help is-danger"><%= error_tag cf, :category %></p>
  <% end %>
</div>

Update link to form as suggested by @benwilson512

https://github.com/wolfiton/blog_api/blob/master/lib/blog_api_web/templates/article/form.html.eex

Update error trace

[info] GET /articles/new
[]
[debug] Processing with BlogApiWeb.ArticleController.new/2
  Parameters: %{}
  Pipelines: [:browser]
[debug] QUERY OK source="categories" db=2.3ms
SELECT c0."id", c0."name", c0."slug", c0."inserted_at", c0."updated_at" FROM "categories" AS c0 []
[info] Sent 500 in 166ms
[error] #PID<0.840.0> running BlogApiWeb.Endpoint (connection #PID<0.839.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /articles/new
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in Keyword.pop/3
        (elixir) lib/keyword.ex:977: Keyword.pop(#Function<2.44023343/1 in BlogApiWeb.ArticleView."form.html"/1>, :value, nil)
        (phoenix_html) lib/phoenix_html/form.ex:1117: Phoenix.HTML.Form.selected/3
        (phoenix_html) lib/phoenix_html/form.ex:1099: Phoenix.HTML.Form.select/4
        (blog_api) lib/blog_api_web/templates/article/form.html.eex:29: anonymous fn/2 in BlogApiWeb.ArticleView."form.html"/1
        (phoenix_html) lib/phoenix_html/form.ex:377: Phoenix.HTML.Form.form_for/4
        (blog_api) lib/blog_api_web/templates/article/form.html.eex:1: BlogApiWeb.ArticleView."form.html"/1
        (blog_api) lib/blog_api_web/templates/article/new.html.eex:3: BlogApiWeb.ArticleView."new.html"/1
        (blog_api) lib/blog_api_web/templates/layout/app.html.eex:15: BlogApiWeb.LayoutView."app.html"/1
        (phoenix) lib/phoenix/view.ex:410: Phoenix.View.render_to_iodata/3
        (phoenix) lib/phoenix/controller.ex:729: Phoenix.Controller.__put_render__/5
        (phoenix) lib/phoenix/controller.ex:746: Phoenix.Controller.instrument_render_and_send/4
        (blog_api) lib/blog_api_web/controllers/article_controller.ex:1: BlogApiWeb.ArticleController.action/2
        (blog_api) lib/blog_api_web/controllers/article_controller.ex:1: BlogApiWeb.ArticleController.phoenix_controller_pipeline/2
        (phoenix) lib/phoenix/router.ex:288: Phoenix.Router.__call__/2
        (blog_api) lib/blog_api_web/endpoint.ex:1: BlogApiWeb.Endpoint.plug_builder_call/2
        (blog_api) lib/plug/debugger.ex:122: BlogApiWeb.Endpoint."call (overridable 3)"/2
        (blog_api) lib/blog_api_web/endpoint.ex:1: BlogApiWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy) /home/dan/Codes/blog_api/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) /home/dan/Codes/blog_api/deps/cowboy/src/cowboy_stream_h.erl:320: :cowboy_stream_h.execute/3

Update article controller
https://github.com/wolfiton/blog_api/blob/master/lib/blog_api_web/controllers/article_controller.ex
Update migrations for articles and categories
https://github.com/wolfiton/blog_api/blob/master/priv/repo/migrations/20191203154052_create_articles.exs
https://github.com/wolfiton/blog_api/blob/master/priv/repo/migrations/20191213160415_create_categories.exs

Schemas and contexts

https://github.com/wolfiton/blog_api/blob/master/lib/blog_api/blog/article.ex

https://github.com/wolfiton/blog_api/blob/master/lib/blog_api/categories/category.ex

https://github.com/wolfiton/blog_api/blob/master/lib/blog_api/blog.ex

https://github.com/wolfiton/blog_api/blob/master/lib/blog_api/categories.ex

Also if there are any suggestions for the rest of my code the full code here https://github.com/wolfiton/blog_api
Thanks in advance

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I’m not really sure I understand why you need a nested form at all. :category_id is a field of the changeset. Nested forms are necessary when you are editing a value AND an associated (or embedded) value. Your form only messes with the top level changeset.

Either way, select does not create a nested form, you want inputs_for for that.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Please provide the full stack trace of the error, and it would probably help to link to the form within that code base.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If what you want is to edit the category_id field, just treat it like the :title field.

<%= select f, :category_id, @categories %>
<p class="help is-danger"><%= error_tag f, :category %></p>

Last Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If what you want is to edit the category_id field, just treat it like the :title field.

<%= select f, :category_id, @categories %>
<p class="help is-danger"><%= error_tag f, :category %></p>

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement