sodapopcan

sodapopcan

Confusion around LiveView initial render/using a map in assigns

To get to the crux of this question right away: Does LiveView always do a first render without any assigns?

So I’m using a struct as one of my assigns. Imagine I’m the url being something like https://mydomain.com/some_id:

defmodule MyAppWeb.HomeLive do
  use MyAppWeb, :live_view

  @default_assigns [
    record: nil, # The default here will never be used, hence `nil`.  I tried giving it a default and it doesn't solve my problem.
    another_key: "that has a dynamic value but needs a default"
  ]

  def mount(%{"record_id" => record_id}, _session, socket) do
    record = Records.find_record(record_id)
    {:ok, assign(socket, @default_assigns |> Keyword.replace!(:record, record))}
  end
end

Then in my template:

<div><%= @record.some_key %></div>

No matter what, even though my page loads seemingly without a hitch, I always get an error that “nil.some_key is not a function”.

I can, of course, fix this with:

<div<%= @record && @record.some_key %></div>

but I’m confused about the lifecycle here… does the template always render once without been passed any assigns? OR am I just doing something completely wrong?

Thanks!

Most Liked

sodapopcan

sodapopcan

Thank you both for your responses. I realize it’s because I’m doing something pretty cavalier that isn’t well represented in my sample code. It’s because my route matches on any root path, so /:record_id. This of course means that requests for favicon.ico is matching :face_with_hand_over_mouth:

hectorsq

hectorsq

Hi,

The first param for assign should be a socket.

{:ok, assign(socket, @default_assigns |> Keyword.replace!(:record, record))}

hectorsq

hectorsq

You also defined @defaul_assigns (missing t) and are using @default_assigns

No, the first render uses the data in assigns that is initialized in mount

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
ovidiubadita
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement