Making a reusable component to display all fields from a schema and incrementally update
- Dominant language
- Elixir
- Stars
- 87
- Forks
- 12
- Avg merge
- 21h 40m
- Merged PRs (30d)
- 2
Description
In making the form component from https://github.com/dwyl/adoro/issues/108 reusable, I came across a few issues:
What we need: A function that can be called with a schema, that will autogenerate a form.
To make this generic, and usable for different projects, we need to either be able to access the View of the calling module, or pass the individual functions from the view (or the result of those functions) to the template. For example, we need to be able to call `user_path` in the template, but this function is defined in the `UserWeb` view. So we would either need to be able to `use UserWeb, :view`, pass the `user_path` function to the template, or pass the result of `user_path` to the template.
I had trouble managing to `use` the view of the calling module:
- To access the name of the calling module, the function would need to be a macro
- Once implementing it as a macro, I could access the name of the module, but could not `use` it. I got the following error:
```
== Compilation error in file lib/migrate_web/controllers/user_controller.ex ==
** (ArgumentError) invalid arguments for use, expected a compile time atom or alias, got: web_name
```
I then decided to try to pass down the result of the `user_path` function as an assign to the template. This worked, I was able to access the path in the template.
I then had to make sure I included all of the Phoenix View code that would normally come from something like `UserWeb, :view`:
``` elixir
use Phoenix.View, root: "lib/templates"
use Phoenix.HTML
import Phoenix.HTML.Form
import ReusableWeb.ErrorHelpers
```
But I then encountered the following error:
```
** (UndefinedFunctionError) function Phoenix.HTML.form_for/3 is undefined or private
(phoenix_html) Phoenix.HTML.form_for(#Ecto.Changeset, valid?: true>, "/users/1", #Function<0.64785437/1 in Reusable.ReusableView."form.html"/1>)
```
It looks like I may have not included the Phoenix Ecto functions that implement the necessary protocol for the changeset to be used in `form_for`: https://github.com/phoenixframework/phoenix_html/issues/85, so I'll look at that next
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.