-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mix phoenix.gen.html User users name:string email:string
- Loading branch information
Showing
11 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
phoenix_blog/priv/repo/migrations/20170420191859_create_user.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule PhoenixBlog.Repo.Migrations.CreateUser do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:users) do | ||
add :name, :string | ||
add :email, :string | ||
|
||
timestamps() | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
defmodule PhoenixBlog.UserControllerTest do | ||
use PhoenixBlog.ConnCase | ||
|
||
alias PhoenixBlog.User | ||
@valid_attrs %{email: "some content", name: "some content"} | ||
@invalid_attrs %{} | ||
|
||
test "lists all entries on index", %{conn: conn} do | ||
conn = get conn, user_path(conn, :index) | ||
assert html_response(conn, 200) =~ "Listing users" | ||
end | ||
|
||
test "renders form for new resources", %{conn: conn} do | ||
conn = get conn, user_path(conn, :new) | ||
assert html_response(conn, 200) =~ "New user" | ||
end | ||
|
||
test "creates resource and redirects when data is valid", %{conn: conn} do | ||
conn = post conn, user_path(conn, :create), user: @valid_attrs | ||
assert redirected_to(conn) == user_path(conn, :index) | ||
assert Repo.get_by(User, @valid_attrs) | ||
end | ||
|
||
test "does not create resource and renders errors when data is invalid", %{conn: conn} do | ||
conn = post conn, user_path(conn, :create), user: @invalid_attrs | ||
assert html_response(conn, 200) =~ "New user" | ||
end | ||
|
||
test "shows chosen resource", %{conn: conn} do | ||
user = Repo.insert! %User{} | ||
conn = get conn, user_path(conn, :show, user) | ||
assert html_response(conn, 200) =~ "Show user" | ||
end | ||
|
||
test "renders page not found when id is nonexistent", %{conn: conn} do | ||
assert_error_sent 404, fn -> | ||
get conn, user_path(conn, :show, -1) | ||
end | ||
end | ||
|
||
test "renders form for editing chosen resource", %{conn: conn} do | ||
user = Repo.insert! %User{} | ||
conn = get conn, user_path(conn, :edit, user) | ||
assert html_response(conn, 200) =~ "Edit user" | ||
end | ||
|
||
test "updates chosen resource and redirects when data is valid", %{conn: conn} do | ||
user = Repo.insert! %User{} | ||
conn = put conn, user_path(conn, :update, user), user: @valid_attrs | ||
assert redirected_to(conn) == user_path(conn, :show, user) | ||
assert Repo.get_by(User, @valid_attrs) | ||
end | ||
|
||
test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do | ||
user = Repo.insert! %User{} | ||
conn = put conn, user_path(conn, :update, user), user: @invalid_attrs | ||
assert html_response(conn, 200) =~ "Edit user" | ||
end | ||
|
||
test "deletes chosen resource", %{conn: conn} do | ||
user = Repo.insert! %User{} | ||
conn = delete conn, user_path(conn, :delete, user) | ||
assert redirected_to(conn) == user_path(conn, :index) | ||
refute Repo.get(User, user.id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule PhoenixBlog.UserTest do | ||
use PhoenixBlog.ModelCase | ||
|
||
alias PhoenixBlog.User | ||
|
||
@valid_attrs %{email: "some content", name: "some content"} | ||
@invalid_attrs %{} | ||
|
||
test "changeset with valid attributes" do | ||
changeset = User.changeset(%User{}, @valid_attrs) | ||
assert changeset.valid? | ||
end | ||
|
||
test "changeset with invalid attributes" do | ||
changeset = User.changeset(%User{}, @invalid_attrs) | ||
refute changeset.valid? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule PhoenixBlog.UserController do | ||
use PhoenixBlog.Web, :controller | ||
|
||
alias PhoenixBlog.User | ||
|
||
def index(conn, _params) do | ||
users = Repo.all(User) | ||
render(conn, "index.html", users: users) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = User.changeset(%User{}) | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"user" => user_params}) do | ||
changeset = User.changeset(%User{}, user_params) | ||
|
||
case Repo.insert(changeset) do | ||
{:ok, _user} -> | ||
conn | ||
|> put_flash(:info, "User created successfully.") | ||
|> redirect(to: user_path(conn, :index)) | ||
{:error, changeset} -> | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
user = Repo.get!(User, id) | ||
render(conn, "show.html", user: user) | ||
end | ||
|
||
def edit(conn, %{"id" => id}) do | ||
user = Repo.get!(User, id) | ||
changeset = User.changeset(user) | ||
render(conn, "edit.html", user: user, changeset: changeset) | ||
end | ||
|
||
def update(conn, %{"id" => id, "user" => user_params}) do | ||
user = Repo.get!(User, id) | ||
changeset = User.changeset(user, user_params) | ||
|
||
case Repo.update(changeset) do | ||
{:ok, user} -> | ||
conn | ||
|> put_flash(:info, "User updated successfully.") | ||
|> redirect(to: user_path(conn, :show, user)) | ||
{:error, changeset} -> | ||
render(conn, "edit.html", user: user, changeset: changeset) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
user = Repo.get!(User, id) | ||
|
||
# Here we use delete! (with a bang) because we expect | ||
# it to always work (and if it does not, it will raise). | ||
Repo.delete!(user) | ||
|
||
conn | ||
|> put_flash(:info, "User deleted successfully.") | ||
|> redirect(to: user_path(conn, :index)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule PhoenixBlog.User do | ||
use PhoenixBlog.Web, :model | ||
|
||
schema "users" do | ||
field :name, :string | ||
field :email, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc """ | ||
Builds a changeset based on the `struct` and `params`. | ||
""" | ||
def changeset(struct, params \\ %{}) do | ||
struct | ||
|> cast(params, [:name, :email]) | ||
|> validate_required([:name, :email]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h2>Edit user</h2> | ||
|
||
<%= render "form.html", changeset: @changeset, | ||
action: user_path(@conn, :update, @user) %> | ||
|
||
<%= link "Back", to: user_path(@conn, :index) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<%= form_for @changeset, @action, fn f -> %> | ||
<%= if @changeset.action do %> | ||
<div class="alert alert-danger"> | ||
<p>Oops, something went wrong! Please check the errors below.</p> | ||
</div> | ||
<% end %> | ||
|
||
<div class="form-group"> | ||
<%= label f, :name, class: "control-label" %> | ||
<%= text_input f, :name, class: "form-control" %> | ||
<%= error_tag f, :name %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= label f, :email, class: "control-label" %> | ||
<%= text_input f, :email, class: "form-control" %> | ||
<%= error_tag f, :email %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= submit "Submit", class: "btn btn-primary" %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<h2>Listing users</h2> | ||
|
||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Email</th> | ||
|
||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%= for user <- @users do %> | ||
<tr> | ||
<td><%= user.name %></td> | ||
<td><%= user.email %></td> | ||
|
||
<td class="text-right"> | ||
<%= link "Show", to: user_path(@conn, :show, user), class: "btn btn-default btn-xs" %> | ||
<%= link "Edit", to: user_path(@conn, :edit, user), class: "btn btn-default btn-xs" %> | ||
<%= link "Delete", to: user_path(@conn, :delete, user), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<%= link "New user", to: user_path(@conn, :new) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h2>New user</h2> | ||
|
||
<%= render "form.html", changeset: @changeset, | ||
action: user_path(@conn, :create) %> | ||
|
||
<%= link "Back", to: user_path(@conn, :index) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<h2>Show user</h2> | ||
|
||
<ul> | ||
|
||
<li> | ||
<strong>Name:</strong> | ||
<%= @user.name %> | ||
</li> | ||
|
||
<li> | ||
<strong>Email:</strong> | ||
<%= @user.email %> | ||
</li> | ||
|
||
</ul> | ||
|
||
<%= link "Edit", to: user_path(@conn, :edit, @user) %> | ||
<%= link "Back", to: user_path(@conn, :index) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule PhoenixBlog.UserView do | ||
use PhoenixBlog.Web, :view | ||
end |