Hi,
Newbie here. I have a User model as follows
defmodule HelloPhoenix.User do
use HelloPhoenix.Web, :model
schema "users" do
field :login, :string
field :email, :string
field :firstname, :string
field :last_name, :string
field :member_type, :string
field :end_date, Ecto.DateTime
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`."""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:login, :email, :firstname, :last_name, :member_type, :end_date])
|> validate_required([:login, :email, :firstname, :last_name, :member_type])
end
end
I want to make sure that the end_date field is calculated either by the user_controller or by the database itself before the insertion. I tried following “create” controller action to create a new user but it is not working
def create(conn, %{"user" => user_params}) do
Map.put(user_params, "end_date", DateTime.utc_now)
changeset = User.changeset(%User{}, user_params)
case Repo.insert(changeset) do
{:ok, _user} ->
conn
|> put_flash(:info, "User created successfully.")
|> put_flash(:info, user_params["end_date"])
|> redirect(to: user_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
and then I disable the end_date field from the form.html.eex file. I was hoping that the Map.put command should be able to insert the key “end_date”. However it doesn’t work.
Any suggestions?
Thanks
Sudheer