Skip to content

Commit f1a0c84

Browse files
authored
Merge pull request #2 from shynome/phoenix-1.5.9-2
将教程更新为 phoenix 1.5.9 可用的版本
2 parents ede9c78 + 0931446 commit f1a0c84

21 files changed

+773
-867
lines changed

00-prepare/00-prepare.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ $ mix local.hex --force
4747
## 安装 Phoenix
4848

4949
```bash
50-
$ mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
50+
mix archive.install hex phx_new 1.5.9
5151
```
5252

5353
## 安装 Node.js(>=5.0.0)

04-user-register/00-prepare.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,35 @@
4242

4343
但这样的手动添加过程太麻烦,还容易出错,应该有便捷的方法。
4444

45-
是的,Phoenix 提供了一系列的 mix 工具包。我们要接触的这个是 [`mix phoenix.gen.html`](https://hexdocs.pm/phoenix/Mix.Tasks.Phoenix.Gen.Html.html)
45+
是的,Phoenix 提供了一系列的 mix 工具包。我们要接触的这个是 [`mix phx.gen.html`](https://hexdocs.pm/phoenix/Mix.Tasks.Phoenix.Gen.Html.html)
4646

47-
请在命令行窗口下切换到 `tv_recipe` 目录,然后执行 `mix phoenix.gen.html` 命令:
47+
请在命令行窗口下切换到 `tv_recipe` 目录,然后执行 `mix phx.gen.html` 命令:
4848

4949
```
5050
$ cd tv_recipe
51-
$ mix phoenix.gen.html User users username:string:unique email:string:unique password:string
51+
$ mix phx.gen.html Users User users username:string:unique email:string:unique password:string
5252
```
53-
![mix phoenix.gen.html 命令](/img/02-mix-phoenix.gen.html.png)
53+
![mix phx.gen.html 命令](/img/02-mix-phoenix.gen.html.png)
5454

5555
执行命令后的输出如下:
5656

5757
```bash
58-
* creating web/controllers/user_controller.ex
59-
* creating web/templates/user/edit.html.eex
60-
* creating web/templates/user/form.html.eex
61-
* creating web/templates/user/index.html.eex
62-
* creating web/templates/user/new.html.eex
63-
* creating web/templates/user/show.html.eex
64-
* creating web/views/user_view.ex
65-
* creating test/controllers/user_controller_test.exs
66-
* creating web/models/user.ex
67-
* creating test/models/user_test.exs
58+
* creating lib/tv_recipe_web/controllers/user_controller.ex
59+
* creating lib/tv_recipe_web/templates/user/edit.html.eex
60+
* creating lib/tv_recipe_web/templates/user/form.html.eex
61+
* creating lib/tv_recipe_web/templates/user/index.html.eex
62+
* creating lib/tv_recipe_web/templates/user/new.html.eex
63+
* creating lib/tv_recipe_web/templates/user/show.html.eex
64+
* creating lib/tv_recipe_web/views/user_view.ex
65+
* creating test/lib/tv_recipe_web/controllers/user_controller_test.exs
66+
* creating lib/tv_recipe/users/user.ex
6867
* creating priv/repo/migrations/20170123145857_create_user.exs
68+
* creating lib/tv_recipe/users.ex
69+
* injecting lib/tv_recipe/users.ex
70+
* creating test/lib/tv_recipe/users_test.exs
71+
* injecting test/tv_recipe/users_test.exs
6972

70-
Add the resource to your browser scope in web/router.ex:
73+
Add the resource to your browser scope in lib/tv_recipe_web/router.ex:
7174

7275
resources "/users", UserController
7376

@@ -122,13 +125,13 @@ Generated tv_recipe app
122125
11:08:12.067 [info] == Migrated in 0.0s
123126
```
124127

125-
操作完上述两步后,因为某些编辑器可能导致的代码重载问题,你需要重启 Phoenix 服务器 - 按两次 Ctrl-C,然后重新执行 `mix phoenix.server`
128+
操作完上述两步后,因为某些编辑器可能导致的代码重载问题,你需要重启 Phoenix 服务器 - 按两次 Ctrl-C,然后重新执行 `mix phx.server`
126129

127130
之后在浏览器中打开网址 `http://localhost:4000/users/new`
128131

129132
![创建用户页面截图](/img/04-users-new-page.png)
130133

131-
有了。是不是很惊讶?我们用 `mix phoenix.gen.html` 命令生成的样板,功能已经很完善:增删改查功能全都有了。我们需要的,只是在样板基础上做点修改。
134+
有了。是不是很惊讶?我们用 `mix phx.gen.html` 命令生成的样板,功能已经很完善:增删改查功能全都有了。我们需要的,只是在样板基础上做点修改。
132135

133136
[接下来](/04-user-register/01-username-required.md)几章,我们将一步步完成本章开头列出的限制条件。
134137

04-user-register/01-username-required.md

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# username 必填
22

3-
[上一章](/04-user-register/00-prepare.md)里,我们用 `mix phoenix.gen.html` 命令创建出完整用户界面,并且具备增加、删除、更改、查询用户的功能。
3+
[上一章](/04-user-register/00-prepare.md)里,我们用 `mix phx.gen.html` 命令创建出完整用户界面,并且具备增加、删除、更改、查询用户的功能。
44

55
这一章,我们将实现 `username` 的第一个规则:`username` 必填,如果未填写,提示用户`请填写`
66

@@ -20,7 +20,7 @@
2020
让我们加上试试:
2121

2222
```elixir
23-
diff --git a/web/models/user.ex b/web/models/user.ex
23+
diff --git a/tv_recipe/users/user.ex b/tv_recipe/users/user.ex
2424
index b7713a0..87ce321 100644
2525
--- a/web/models/user.ex
2626
+++ b/web/models/user.ex
@@ -43,7 +43,7 @@ index b7713a0..87ce321 100644
4343

4444
又或者,我们可以用 Phoenix 生成的测试文件来验证。
4545

46-
打开 `test/models/user_test.exs` 文件,默认内容如下:
46+
打开 `test/tv_recipe/users_test.exs` 文件,默认内容如下:
4747

4848
```elixir
4949
defmodule TvRecipe.UserTest do
@@ -68,10 +68,10 @@ end
6868
文件中有两个变量,`@valid_attrs` 表示有效的 `User` 属性,`@invalid_attrs` 表示无效的 `User` 属性,我们按本章开头拟定的规则修改 `@valid_attrs`
6969

7070
```elixir
71-
diff --git a/test/models/user_test.exs b/test/models/user_test.exs
71+
diff --git a/test/tv_recipe/users_test.exs b/test/tv_recipe/users_test.exs
7272
index 1d5494f..7c73207 100644
73-
--- a/test/models/user_test.exs
74-
+++ b/test/models/user_test.exs
73+
--- a/test/tv_recipe/users_test.exs
74+
+++ b/test/tv_recipe/users_test.exs
7575
@@ -3,7 +3,7 @@ defmodule TvRecipe.UserTest do
7676

7777
alias TvRecipe.User
@@ -83,42 +83,53 @@ index 1d5494f..7c73207 100644
8383
test "changeset with valid attributes" do
8484
```
8585

86-
接着,在 `user_test.exs` 文件中添加一个新测试:
86+
接着,在 `users_test.exs` 文件中添加一个新测试:
8787

8888
```elixir
8989
diff --git a/test/models/user_test.exs b/test/models/user_test.exs
9090
index 7c73207..4c174ab 100644
91-
--- a/test/models/user_test.exs
92-
+++ b/test/models/user_test.exs
91+
--- a/test/tv_recipe/users_test.exs
92+
+++ b/test/tv_recipe/users_test.exs
9393
@@ -15,4 +15,9 @@ defmodule TvRecipe.UserTest do
9494
changeset = User.changeset(%User{}, @invalid_attrs)
9595
refute changeset.valid?
9696
end
9797
+
9898
+ test "username should not be blank" do
9999
+ attrs = %{@valid_attrs | username: ""}
100-
+ assert {:username, "请填写"} in errors_on(%User{}, attrs)
100+
+ assert %{username: ["请填写"] } = errors_on(%User{}, attrs)
101101
+ end
102102
end
103103
```
104104

105105
这里,`%{@valid_attrs | username: ""}` 是 Elixir 更新映射(Map)的一个方法。
106106

107-
至于 `errors_on` 函数,它定义在 `tv_recipe/test/support/model_case.ex` 文件中:
107+
至于 `errors_on/2` 函数,它需要新增在 `test/support/data_case.ex` 文件中:
108108

109109
```elixir
110-
def errors_on(struct, data) do
111-
struct.__struct__.changeset(struct, data)
112-
|> Ecto.Changeset.traverse_errors(&TvRecipe.ErrorHelpers.translate_error/1)
113-
|> Enum.flat_map(fn {key, errors} -> for msg <- errors, do: {key, msg} end)
110+
def errors_on(changeset) do
111+
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
112+
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
113+
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
114+
end)
115+
end)
116+
end
117+
+
118+
+ def errors_on(struct, attrs) do
119+
+ changeset = struct.__struct__.changeset(struct, attrs)
120+
+ errors_on(changeset)
121+
+ end
114122
end
115123
```
124+
125+
是否很吃惊?要知道,如果是在 JavaScript 里写两个同名函数,后一个函数会覆盖前一个的定义,而 Elixir 下,我们可以定义多个同名函数,它们能处理不同的状况,而又互不干扰。
126+
116127
它检查给定数据中的错误消息,并返回给我们。
117128

118129
现在在命令行下运行:
119130

120131
```bash
121-
$ mix test test/models/user_test.exs
132+
$ mix test test/tv_recipe/users_test.exs
122133
```
123134
结果如下:
124135

0 commit comments

Comments
 (0)