Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example app post index controller error #47

Closed
mhussa opened this issue Feb 27, 2017 · 3 comments
Closed

Example app post index controller error #47

mhussa opened this issue Feb 27, 2017 · 3 comments

Comments

@mhussa
Copy link

mhussa commented Feb 27, 2017

I got the following error trying out the example app:

[debug] Processing by Example.Admin.PostController.index/2
  Parameters: %{}
  Pipelines: [:browser]
[debug] QUERY OK source="authors" db=0.9ms queue=2.8ms
SELECT a0."id", a0."name", a0."email", a0."inserted_at", a0."updated_at" FROM "authors" AS a0 []
[%Example.Author{__meta__: #Ecto.Schema.Metadata<:loaded, "authors">,
  email: "blee", id: 5, inserted_at: #Ecto.DateTime<2017-02-27 16:46:51>,
  name: "blee", updated_at: #Ecto.DateTime<2017-02-27 16:46:51>}]
[info] Sent 500 in 16ms
[error] #PID<0.541.0> running Example.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /admin/posts
** (exit) an exception was raised:
    ** (KeyError) key :author not found in: %Example.Author{__meta__: #Ecto.Schema.Metadata<:loaded, "authors">, email: "blee", id: 5, inserted_at: #Ecto.DateTime<2017-02-27 16:46:51>, name: "blee", updated_at: #Ecto.DateTime<2017-02-27 16:46:51>}
        (example) web/controllers/admin/post_controller.ex:103: anonymous fn/1 in Example.Admin.PostController.assign_authors/2
        (elixir) lib/enum.ex:1229: Enum."-map/2-lists^map/1-0-"/2
        (example) web/controllers/admin/post_controller.ex:103: Example.Admin.PostController.assign_authors/2

It only happens when there is at least one author or category. If there are no author or category records the error does not occur.

The problem in the example code occurs here: torch/apps/example/web/controllers/admin/post_controller.ex

  defp assign_authors(conn, _opts) do
    authors =
      Example.Author
      |> Repo.all
     |> Enum.map(&({&1.author, &1.id}))
    assign(conn, :authors, authors)
end

|> Enum.map(&({&1.author, &1.id}))

"author" is the name of the table not the field "name" which gets loaded into conn for the filter. Changing &1.author to &1.name and doing the same for assign_categories makes the Post index work when an Author or Category has been defined.

The codegen assoc_plug_definitions(binding, attrs) in torch/lib/mix/tasks/torch.gen.ex uses :display_name. I've not looked any deeper, I'm a bit out of my depth at this point.

Thanks for making torch btw 👍

@jamonholmgren
Copy link
Contributor

@mhussa Thanks for the issue! We'll take a look soon. cc @yulolimum @danielberkompas @zberkom

@danielberkompas
Copy link
Contributor

I can't reproduce this. Maybe try on latest and mix ecto.reset inside example?
admin

@mhussa
Copy link
Author

mhussa commented Mar 14, 2017

Fixed by #22, so the regenerated code is now correct.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants