legibly-sinless-fil

legibly-sinless-fil

Phoenix LiveView mount does not get called

I have tried creating a fresh project using LiveView. I am following this tutorial : https://www.youtube.com/watch?v=qpaFivCmJOY. I have followed the installation instruction manual and created a basic controller for my view, that contains a live_render tag, referencing a LiveView controller.

Template :

<h1>Projects</h1>                                                                                                                                                                                                                                                           
<%= live_render(@conn, TodoistHelperWeb.AppController) %>

Normal controller :

defmodule TodoistHelperWeb.PageController do                                                                                                                                                                                                                                
  use TodoistHelperWeb, :controller                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                            
  def index(conn, _params) do                                                                                                                                                                                                                                               
    render(conn, "index.html")                                                                                                                                                                                                                                              
  end                                                                                                                                                                                                                                                                       
end 

LiveView controller :

defmodule TodoistHelperWeb.AppController do                                                                                                                                                                                                                                 
  use Phoenix.LiveView                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                            
  def mount(_session, socket) do                                                                                                                                                                                                                                            
    {:ok, assign(socket, projects: ["project1"])}                                                                                                                                                                                                                                  
  end                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                            
  def render(assigns) do                                                                                                                                                                                                                                                    
      TodoistHelperWeb.AppView.render("projects.html", assigns)                                                                                                                                                                                                             
  end                                                                                                                                                                                                                                                                       
end

LiveView template :

<%= for project <- @projects do %>                                                                                                                                                                                                                                          
<div><%= project %></div>                                                                                                                                                                                                                                                   
<% end %>

The project compiles correctly, everything seems to work, but the @projects variable cannot be found in the liveview template! If I replace it with a static list, the page is displayed without any errors. Using print statements, I was able to determine that render is properly called, however mount is not.

What did I miss? Did I mess up one of the installation steps?

Marked As Solved

josevalim

josevalim

Creator of Elixir

It should be mount/3:

@impl true
def mount(_params, _session, socket) do
  {:ok, assign(socket, projects: ["project1"])}
end

If you put @impl true whenever you are implementing a callback, Elixir will let you know if you got the wrong name or the wrong number of arguments. :slight_smile:

Last Post!

legibly-sinless-fil

legibly-sinless-fil

Oh god that was pretty dump -_-… Thanks!

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
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

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement