Skip to content
Permalink
Browse files

Creating Users

  • Loading branch information...
dnsbty committed Aug 18, 2019
1 parent 42309f0 commit befb98d4bc45a0226b94241184ea3f48663e9fd2
@@ -1,4 +1,5 @@
defmodule UserAdmin.Users do
alias UserAdmin.Repo
alias UserAdmin.Users.User

@doc """
@@ -8,4 +9,14 @@ defmodule UserAdmin.Users do
def change_user(user, changes \\ %{}) do
User.changeset(user, changes)
end

@doc """
Create a user.
"""
@spec create_user(map()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
def create_user(attrs) do
%User{}
|> User.changeset(attrs)
|> Repo.insert()
end
end
@@ -3,6 +3,7 @@ defmodule UserAdminWeb.UserLive.New do
alias UserAdminWeb.UserView
alias UserAdmin.Users
alias UserAdmin.Users.User
alias UserAdminWeb.Router.Helpers, as: Routes

def mount(_session, socket) do
changeset = Users.change_user(%User{})
@@ -13,4 +14,19 @@ defmodule UserAdminWeb.UserLive.New do
def render(assigns) do
UserView.render("new.html", assigns)
end

def handle_event("create", %{"user" => attrs}, socket) do
case Users.create_user(attrs) do
{:ok, user} ->
socket =
socket
|> put_flash(:info, "User successfully created")
|> redirect(to: Routes.live_path(socket, __MODULE__))

{:noreply, socket}

{:error, changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end
@@ -1,7 +1,9 @@
<h2>New User</h2>
<%= f = form_for @changeset, "#", phx_submit: :create %>
<fieldset>
<%= label f, :name, for: "nameField" %>
<%= text_input f, :name, id: "nameField" %>
<%= error_tag f, :name %>

<label for="roleField">Role</label>
<select id="roleField">

0 comments on commit befb98d

Please sign in to comment.
You can’t perform that action at this time.