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

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement