Hello folks!
Like the title says right now I’m feeling a bit frustrated by the inputs_for
helper.
It’s not the first time I’m bumping into this problem, maybe already the second or third time, so probably there’s something I don’t understand quite well. I would love to have your advice on that
I’m coming from the Rails ecosystem, and with Rails and its fields_for
helper, you can basically pass an attribute name OR a random attribute and any collection as arguments to it, and Rails will create a nested form iterating on the given association name OR attribute and collection.
This is very useful, because sometimes you don’t want to just iterate basically on a many
association, but on a filtered sub-set for example. And with the Rails helper, it’s quite easy to do this simple filtering directly in the view.
Right now, in Phoenix, it looks like inputs_for
can just take an attribute from the underlying form data. So it’s not really easy to simply filter elements from a has_many
relationship on the view or template level.
I would need to create a different changeset, and even a different underlying struct for it. OK, I can still do that…
But what’s more annoying is that if this attribute is not a real schema association, it raises an exception Check the field exists and it is one of embeds_one, embeds_many, has_one, has_many, belongs_to or many_to_many
.
So how is it possible to just use inputs_for
on a virtual field, a simple list of Structs, or something that is not related to the DB?
Would it be difficult to make it accept an optional simple list of Struct as an argument on which it would iterate like the Rails fields_for
helper?