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

Making a reusable component to display all fields from a schema and incrementally update #6

Open
Danwhy opened this issue Sep 25, 2018 · 3 comments
Labels
enhancement New feature or request

Comments

@Danwhy
Copy link
Member

Danwhy commented Sep 25, 2018

In making the form component from dwyl/adoro#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:

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<action: nil, changes: %{}, errors: [], data: #Migrate.Accounts.User<>, 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: phoenixframework/phoenix_html#85, so I'll look at that next

@nelsonic nelsonic added the enhancement New feature or request label Sep 25, 2018
@Danwhy
Copy link
Member Author

Danwhy commented Sep 25, 2018

It turns out the function should have been Phoenix.Html.Form.form_for

@Danwhy
Copy link
Member Author

Danwhy commented Sep 25, 2018

@nelsonic I believe I have this fully working now, should I open a PR in this repo?

@nelsonic
Copy link
Member

@Danwhy yeah, go for it. 👍

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

No branches or pull requests

2 participants