How to Iterate and Render Database Data in Phoenix LiveView

From ElixirBlocks
Revision as of 02:36, 29 May 2023 by Admin (talk | contribs)
Jump to: navigation, search

This page is in progress

Before You Begin

Installing Phoenix

This article assumes that you have installed the Phoenix web framework and all its dependencies correctly without errors. If you have not installed the Phoenix web framework please view the documentation here


Example

This code references a PostgreSQL table named TestBeds.

Before

defmodule AppWeb.PageLive do
   use AppWeb, :live_view  
   def mount(_params, _session, socket)  do
	 {:ok, socket}  
   end

   def render(assigns) do
     ~H"""
        Hello World! YAY
	 """ 
   end

end

After

 use AppWeb, :live_view
 alias App.TestBeds

defmodule AppWeb.PageLive do
   use AppWeb, :live_view  
    alias App.TestBeds
   def mount(_params, _session, socket)  do
	 {:ok, assign(socket, testbeds: TestBeds.list_testbeds())}  
   end

   def render(assigns) do
	   ~H"""
		
        <%= for testbed <- assigns.testbeds do %>
		  <div><%= testbed.name %></div>
	    <% end %>

	   """ 
   end
end