I’m trying to create a contact email form with the context of Contact >Emails, however I’m running into changeset problems and I’ve been going round in circles trying to solve it.
The changeset is within my email.ex file:
defmodule Iotc.Contact.Email do
use Ecto.Schema
import Ecto.Changeset
alias Iotc.Contact.Email
schema "contact_emails" do
field :email, :string
field :event, :string
field :message, :string
field :name, :string
timestamps()
end
def changeset(%Email{} = email, attrs) do
email
|> cast(attrs, [:name, :email, :message, :event])
|> validate_required([:name, :email, :message])
end
end
And my email_controller.ex has the following:
defmodule Iotc.Web.EmailController do
use Iotc.Web, :controller
alias Iotc.Contact
def index(conn, _params) do
changeset = Contact.Email.changeset(%Contact.Email{})
emails = Contact.list_emails()
render(conn, "index.html", emails: emails, changeset: changeset)
end
def new(conn, _params) do
changeset = Contact.changeset(%Contact.Email{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"email" => email_params}) do
changeset = Contact.changeset(%Iotc.Contact.Email{}, email_params)
case Contact.create_email(changeset) do
{:ok, email} ->
conn
|> put_flash(:info, "Email sent")
|> redirect(to: email_path(conn, :index))
{:error, changeset} ->
conn
|> put_flash(:error, "Something went wrong")
|> render("index.html", changeset: changeset)
end
end
That current set up gives me function Iotc.Contact.Email.changeset/1 is undefined or private
so I’ve tried changeset = Contact.changeset(%Email{})
as well, but this gives me another error of ib/iotc/web/controllers/email_controller.ex:6: Email.__struct__/1 is undefined, cannot expand struct Email
.
This isp probably so simply, but I’ve now gone blind.