Sebb

Sebb

Can't use for-variable in heex - {if for_var == ...} - Protocol.UndefinedError

Why do I get Protocol.UndefinedError for this code?

~H"""
<%= for index <- [0, 1, 2] do %>
  <div {if 1 == index, do: [selected: ""]} ><%= index %></div>
<% end %>
"""
(Protocol.UndefinedError) protocol Enumerable not implemented for nil of type Atom
elixir          1.12.2-otp-23   /home/sf/.tool-versions
erlang          23.3.4.4        /home/sf/.tool-versions
Phoenix installer v1.6.0

btw, this works:

<div selected={1==index}><%= index %></div>

and produces a boolean attribute

<div>0</div>
<div selected>1</div>
<div>2</div>

nice.

Marked As Solved

Nicd

Nicd

I guess because the if statement will return nil by default if the check is false.

Also Liked

chrismccord

chrismccord

Creator of Phoenix
<div {%{selected: index == 1}}><%= index %></div>
Sebb

Sebb

:roll_eyes:

, else: []

fixes it. thanks guys.

Sebb

Sebb

the shortest I found (by accident) is:

<div boolean_attr={@one == 1}>boolean_attr</div>

I’d like a list of heex-recepies.
Here are some (including stupid ones):

<div {@dynamic_attr}>dynamic_attr from assign</div>
<div {@dynamic_attr} static_attr="is-static">dynamic_attr with static attribute</div>
<div boolean_attr={@one == 1}>boolean_attr</div>
<div {%{boolean_attr: @one == 1}}>boolean_attr from dict</div>
<div {if @one == 1, do: [boolean_attr: ""], else: []}>boolean_attr kw-list from if</div>
<div {if @one == 1, do: %{boolean_attr: ""}, else: %{}}>boolean_attr dict from if</div>
<div some_attr={if @one == 1, do: "one-is-one"}>value from if</div>
<div some_attr={if @one == 0, do: "one-is-zero", else: "one-is-one"}>value from if/else</div>
<div class={"some_class #{if(@one == 1, do: " optional_class", else: "")}"}>interpolation</div>
<div class={"some_class" <> if @one == 1, do: " optional_class", else: ""}>concatenation 1</div>
<div class={"some_class" <> if @one == 0, do: " optional_class", else: ""}>concatenation 2</div>
<div {heex_helper(@some_state)}>dynamic_attr from helper function</div>
<div case={
      case @one do
        1 -> "one-is-one"
        _ -> "one-is-not-one"
      end
      }>
  case
</div>
wanton7

wanton7

I haven’t used HEEx yet but shouldn’t this
<div {if 1 == index, do: [selected: ""]} ><%= index %></div>
be
<div {if 1 == index, do: [selected: ""], else: []} ><%= index %></div>

From here it says it should be keyword list or map Views and templates — Phoenix v1.6.0 because you don’t have else returning [] it’s neither.

wanton7

wanton7

It would be nice if it supported nil as well.

Where Next?

Popular in Questions Top

JDanielMartinez
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Jim
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
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mgjohns61585
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
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

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
nsuchy
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
mgjohns61585
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

We're in Beta

About us Mission Statement