Hello I am new to coding, and thought I would work on a program creating a new patient form to store patient info! I am using the Pragmatic Studio tutorial on form create to create a new patient form. I also used a tailwindui template to get the visual for the form I wanted. Right now my form is empty, and I followed the tutorial to “programmatically create a patient in the database”. As far as I can see I have followed the instructions the best I could while doing my own thing by adding in tailwindui; however, my data is not showing up on localhost4000 when I add it programmatically to the database using an iex session and manually. Any suggestions on why my data is not pulling through?
my iex session looks like this
‘’’
Interactive Elixir (1.12.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> alias Rainier.Patients
Rainier.Patients
iex(2)> alias Rainier.Patients.Patient
Rainier.Patients.Patient
iex(3)> alias Rainier.Repo
Rainier.Repo
iex(4)> p = %Patient{}
%Rainier.Patients.Patient{
meta: ecto.Schema.Metadata<:built, “patients”>,
birth_date: nil,
first_name: nil,
gender: nil,
id: nil,
inserted_at: nil,
last_name: nil,
middle_name: nil,
nick_name: nil,
updated_at: nil
}
iex(5)> attrs = %{first_name: “Kimberly”, middle_name: “Brooke”, last_name: “Johnson”, nick_name: “Brooke”, birth_date: “03-26-1995”, gender: “female”}
%{
birth_date: “03-26-1995”,
first_name: “Kimberly”,
gender: “female”,
last_name: “Johnson”,
middle_name: “Brooke”,
nick_name: “Brooke”
}
iex(6)> changeset = Patient.changeset(p, attrs)
ecto.Changeset<
action: nil,
changes: %{
birth_date: “03-26-1995”,
first_name: “Kimberly”,
gender: “female”,
last_name: “Johnson”,
middle_name: “Brooke”,
nick_name: “Brooke”
},
errors: ,
data: #Rainier.Patients.Patient<>,
valid?: true
‘’’
my patients_live.ex file looks like this:
‘’’
defmodule RainierWeb.PatientsLive do
use RainierWeb, :live_view
alias Rainier.Patients
alias Rainier.Patients.Patient
def mount(_params, _session, socket) do
patients = Patients.list_patients()
changeset = Patients.change_patient(%Patient{})
socket = assign(socket,
patients: patients,
changeset: changeset
)
{:ok, socket, temporary_assigns: [patients: []]}
end
def handle_event(“save”, %{“patient” => params}, socket) do
case Patients.create_patient(params) do
{:ok, patient} ->
socket =
update(
socket,
:patients,
fn patients -> [ patient | patients] end
)
changeset = Patients.change_patient(%Patient{})
socket = assign(socket, changeset: changeset)
:timer.sleep(500)
{:noreply, socket}
{:error, %Ecto.Changeset{} = changeset} ->
socket = assign(socket, changeset: changeset)
{:noreply, socket}
end
end
end
‘’’
my patients_live.html.leex looks like this
‘’’
<tbody>
<%# <!-- Odd row --> %>
<% for patient <- @patients do %>
<%= patient.first_name %>
patient.last_name
<tr class="bg-white">
<td class="px-6 py-4 text-sm text-gray-500 whitespace-nowrap" id="<%= patient.first_name %>">
<%= patient.first_name %>
</td>
<td class="px-6 py-4 text-sm text-gray-500 whitespace-nowrap" id="<%= patient.middle_name %>">
<%= patient.middle_name %>
</td>
<td class="px-6 py-4 text-sm text-gray-500 whitespace-nowrap" id="<%= patient.last_name %>">
<%= patient.last_name %>
</td>
<td class="px-6 py-4 text-sm text-gray-500 whitespace-nowrap" id="<%= patient.nick_name %>">
<%= patient.nick_name %>
</td>
<td class="px-6 py-4 text-sm text-gray-500 whitespace-nowrap" id="<%= patient.birth_date %>">
<%= patient.birth_date %>
</td>
<td class="px-6 py-4 text-sm text-gray-500 whitespace-nowrap" id="<%= patient.gender %>">
<%= patient.gender %>
</td>
<td class="px-6 py-4 text-sm font-medium text-right whitespace-nowrap">
<a href="#" class="text-indigo-600 hover:text-indigo-900">Edit</a>
</td>
</tr>
<% end %>
‘’’
my context module looks like this
‘’’
defmodule Rainier.Patients do
@moduledoc “”"
The Patients context.
“”"
import Ecto.Query, warn: false
alias Rainier.Repo
alias Rainier.Patients.Patient
@doc “”"
Returns the list of patients.
Examples
iex> list_patients()
[%Patient{}, ...]
“”"
def list_patients do
Repo.all(Patient)
end
@doc “”"
Gets a single patient.
Raises Ecto.NoResultsError
if the Patient does not exist.
Examples
iex> get_patient!(123)
%Patient{}
iex> get_patient!(456)
** (Ecto.NoResultsError)
“”"
def get_patient!(id), do: Repo.get!(Patient, id)
@doc “”"
Creates a patient.
Examples
iex> create_patient(%{field: value})
{:ok, %Patient{}}
iex> create_patient(%{field: bad_value})
{:error, %Ecto.Changeset{}}
“”"
def create_patient(attrs \ %{}) do
%Patient{}
|> Patient.changeset(attrs)
|> Repo.insert()
end
@doc “”"
Updates a patient.
Examples
iex> update_patient(patient, %{field: new_value})
{:ok, %Patient{}}
iex> update_patient(patient, %{field: bad_value})
{:error, %Ecto.Changeset{}}
“”"
def update_patient(%Patient{} = patient, attrs) do
patient
|> Patient.changeset(attrs)
|> Repo.update()
end
@doc “”"
Deletes a patient.
Examples
iex> delete_patient(patient)
{:ok, %Patient{}}
iex> delete_patient(patient)
{:error, %Ecto.Changeset{}}
“”"
def delete_patient(%Patient{} = patient) do
Repo.delete(patient)
end
@doc “”"
Returns an %Ecto.Changeset{}
for tracking patient changes.
Examples
iex> change_patient(patient)
%Ecto.Changeset{data: %Patient{}}
“”"
def change_patient(%Patient{} = patient, attrs \ %{}) do
Patient.changeset(patient, attrs)
end
end
‘’’
my Ecto Schema module looks like this
‘’’
defmodule Rainier.Patients.Patient do
use Ecto.Schema
import Ecto.Changeset
schema “patients” do
field :birth_date, :string
field :first_name, :string
field :gender, :string
field :last_name, :string
field :middle_name, :string
field :nick_name, :string
timestamps()
end
@doc false
def changeset(patient, attrs) do
patient
|> cast(attrs, [:first_name, :middle_name, :last_name, :nick_name, :birth_date, :gender])
|> validate_required([:first_name, :middle_name, :last_name, :nick_name, :birth_date, :gender])
end
end
‘’’