mythicalprogrammer

mythicalprogrammer

Phoenix Template: design pattern to check if exist or not

Hi guys.

I’m getting this error because of how I coded my template:

function nil.name/0 is undefined

The offending code:

 <%= @company.country.name %>

What’s elixir idiomatic way of writing this to check if exist display the country name else don’t display and don’t access country.name so I don’t trigger the error? I did an if else before but I feel like there may be something better out there.

Thanks

Marked As Solved

aptinio

aptinio

You can add a country_name/1 function in your View module like so:

def country_name(%{country: %{name: name}}), do: name
def country_name(_), do: "" # or "No country"

Then just:

<%= country_name(company) %>

Also Liked

sfusato

sfusato

You can also use the bracket-based access syntax like this: @company.country[:name]

Furthermore, the bracket-based access syntax transparently ignores nil values. When trying to access anything on a nil value, nil is returned:

This would work just fine, but perhaps won’t transmit intent as good as an if/else or a separate view function would.

NobbZ

NobbZ

For me it seems top work arbitrary deep:

iex(1)> nil[:i][:can][:go][:as][:many][:levels][:i][:want]
nil
LostKobrakai

LostKobrakai

I also like using the view module for doing that kind of stuff, which allows my templates to need less amount of logic. They just display what’s given.

def render("myview.html", assigns) do
    country_name = assigns.company[:country][:name] || "Unavailable"
    render_template("myview.html", Map.merge(assigns, %{country_name: country_name}
end

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
alice
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement