trisolaran
Defining attributes for Phoenix component from a list
I’m not a macro expert, so maybe someone can help me understand this.
I want to define attributes in my function component from a list. I’ve used the following approach successfully with Ecto schemas in the past:
@list_of_fields ~w(field1 field2 field3 ... )a
for field_name <- @list_of_fields do
field field_name, :string
end
But when I try to do the same with a function component:
@list_of_attributes ~w(attr1 attr2 attr3 ... )a
for attr_name <- @list_of_attributes do
attr attr_name, :string
end
I get a lovely:
== Compilation error in file lib/live_select.ex ==
** (FunctionClauseError) no function clause matching in Phoenix.Component."MACRO-attr"/4
The following arguments were given to Phoenix.Component."MACRO-attr"/4:
# 1
#Macro.Env<aliases: [{ChangeMsg, LiveSelect.ChangeMsg}], context: nil, context_modules: [LiveSelect], file: "/Users/max/code/live_select/lib/live_select.ex", function: nil, functions: [{Phoenix.Component, [assign: 2, assign: 3, assign_new: 3, assigns_to_attributes: 1, assigns_to_attributes: 2, changed?: 2, dynamic_tag: 1, focus_wrap: 1, form: 1, inputs_for: 1, intersperse: 1, link: 1, live_component: 1, live_file_input: 1, live_flash: 2, live_img_preview: 1, live_render: 2, live_render: 3, live_title: 1, to_form: 1, to_form: 2, update: 3, upload_errors: 1, upload_errors: 2]}, {Kernel, [!=: 2, !==: 2, *: 2, **: 2, +: 1, +: 2, ++: 2, -: 1, -: 2, --: 2, /: 2, <: 2, <=: 2, ==: 2, ===: 2, =~: 2, >: 2, >=: 2, abs: 1, apply: 2, apply: 3, binary_part: 3, binary_slice: 2, binary_slice: 3, bit_size: 1, byte_size: 1, ceil: 1, div: 2, elem: 2, exit: 1, floor: 1, function_exported?: 3, get_and_update_in: 3, get_in: 2, hd: 1, inspect: 1, inspect: 2, is_atom: 1, is_binary: 1, is_bitstring: 1, ...]}, {Phoenix.LiveView.Helpers, [live_file_input: 2, live_img_preview: 2, live_patch: 1, live_patch: 2, live_redirect: 1, live_redirect: 2, live_title_tag: 1, live_title_tag: 2]}], lexical_tracker: #PID<0.252.0>, line: 262, macro_aliases: [], macros: [{Phoenix.Component.Declarative, [def: 2, defp: 2]}, {Phoenix.Component, [attr: 2, attr: 3, embed_templates: 1, embed_templates: 2, render_slot: 1, render_slot: 2, sigil_H: 2, slot: 1, slot: 2, slot: 3]}, {Kernel, [!: 1, &&: 2, ..: 0, ..: 2, ..//: 3, <>: 2, @: 1, alias!: 1, and: 2, binding: 0, binding: 1, dbg: 0, dbg: 1, dbg: 2, def: 1, defdelegate: 2, defexception: 1, defguard: 1, defguardp: 1, defimpl: 2, defimpl: 3, defmacro: 1, defmacro: 2, defmacrop: 1, defmacrop: 2, defmodule: 2, defoverridable: 1, defp: 1, defprotocol: 2, defstruct: 1, destructure: 2, get_and_update_in: 2, if: 2, in: 2, is_exception: 1, ...]}, {Phoenix.LiveView.Helpers, [live_component: 2, live_component: 3, render_block: 1, render_block: 2, sigil_L: 2]}], module: LiveSelect, requires: [Application, Kernel, Kernel.Typespec, Phoenix.Component, Phoenix.Component.Declarative, Phoenix.LiveView.Helpers, Phoenix.Template], ...>
# 2
{:attr_name, [line: 262], nil}
# 3
:string
# 4
[]
(phoenix_live_view 0.18.13) lib/phoenix_component.ex:1824: Phoenix.Component."MACRO-attr"/4
(phoenix_live_view 0.18.13) expanding macro: Phoenix.Component.attr/2
lib/live_select.ex:262: LiveSelect (module)
Why does it work in one case but not in the other? Is there a way to make it work?
Cheers
Most Liked
LostKobrakai
You can use unquote in module bodies as well.
1
Popular in Questions
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
I would like to know what is the best IDE for elixir development?
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)...
New
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New







