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

Documentation on using Form bindings with many-to-many or belongs-to fields lacking #3197

Closed
ponychicken opened this issue Mar 28, 2024 · 3 comments · May be fixed by phoenixframework/phoenix#5764

Comments

@ponychicken
Copy link
Contributor

In many applications you have some kind of belongs-to or many-to-many associations.
Like a blog post which has many categories, and each category has many blog posts.

It is straightforward to figure out how to make a <.input type="select"> for an Enum,
such as

<.input
          field={@form[:status]}
          type="select"
          label="Status"
          options={Ecto.Enum.values(Post, :status)}
/>

The same approach fails when trying to add a similar dropdown field to select the categories

<.input
          field={@form[:categories]}
          type="select"
          label="Categories"
          options={get_categories()}
/>

with ** (Protocol.UndefinedError) protocol Phoenix.HTML.Safe not implemented for %Category{...

Since this is a common pattern, I would suggest to add some paragraph into guides/client/form-bindings.md to explain the prefered way to solve this.

@ponychicken
Copy link
Contributor Author

In https://hexdocs.pm/phoenix_html_helpers/PhoenixHTMLHelpers.Form.html#multiple_select/4 its quite nicely documented how to do this, but don't find a reference for Phoenix 1.7

@ponychicken
Copy link
Contributor Author

ponychicken commented Mar 28, 2024

Ok, I have to correct myself.
<.input type="select"> is the correct way to do this.

But get_options_for_select will fail if the value is not specified explicitely.

@chrismccord
Copy link
Member

tracking on the phx side. 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

Successfully merging a pull request may close this issue.

2 participants