crusso
I have roles and permissions schemas in many_to_many relationships:
schema "roles" do
field(:name, :string)
many_to_many(:permissions, X.Accounts.Permission, join_through: "permissions_roles")
end
schema "permissions" do
field(:name, :string)
many_to_many(:roles, X.Accounts.Role, join_through: "permissions_roles")
end
In my Roles form, I’d like to have a list of checkboxes of all of the Permissions that could be associated with a Role. Checking a checkbox would mean an entry in the permissions_roles table, which is a database-only relationship that is not represented by a named module.
I’ve considered various ways of solving this, but I feel like I’m writing a lot of code for something that should be simpler.
Is there a pattern for creating a form like this or a good example someone could recommend?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
josevalim
I think the What’s new in Ecto 2.0 ebook covers this?
But in a nutshell, I think you need to do something like this:
crusso
Right, that’s some of the Ecto - but I’m looking for the full idiomatic Phoenix way to handle this problem from end-to-end. It’s similar to the embedded schema problem that you can find multiple web hits for (that typically uses inputs_for in the form), but it has some uniqueness that makes it different:
josevalim
Every time you will show the form, you should load all permissions as well as preload all permissions in the role you are editing right in your controller.
In your view, you will have to traverse the full list of permissions. Something like this:
You should probably put the input generation function in your view but that’s the code in rough lines. Keep in mind that
params["permissions_ids"]will be nil if no checkbox is checked.sweeneydavidj
Hi,
I think we also need to deal with the case where the user while (creating and/or updating) selects some checkboxes and then hits submit but there are are some validation errors caused, perhaps by a field somewhere else on the form. In that case when the page is refreshed the checkboxes that the user just selected should still be checked.
I struggled with this also and the following is what I came up with. Sorry that it’s not very polished (I even have a TODO in there) and I’m sure someone will tell me that I’ve overcooked this but here goes anyway
…
I wanted to have a legend on the left then the checkboxes with the label of each checkbox on the right. Clicking on the checkbox label also selects the checkbox - so the IDs had to match. And my example is of a User with many_to_many Roles.
In the form I have…
And this partial looks like…
Then I have an inputs_helper.ex with the functions that are called from the partial.
Any feedback is welcome!
amarraja
My current application uses multi-select checkboxes all over the place.
I came up with the solution below, which works for both new and updated records, and maintains user selections in the case of errors. It was some of the first code I wrote in phoenix, and specifically using form helpers / changesets so it could do with some cleaning up and a nicer api.
And the helper
crusso
Okay, I’ve managed to put together a solution. One important change to your raw code (besides the closing “%>” tag needed is the addition of brackets to the role[permissions_ids] name so that the returned results comes back as an array.
I think that part of my hesitation was due to not understanding the name mapping from the form to the parameters sent back to the controller.
Thanks!
yessikameliza
I’m doing something similar but I don’t understand where that part of code is going