在開始敲代碼前,我們先確認(rèn)一下,TvRecipe 項目中,用戶有哪些數(shù)據(jù)需要存儲,這些數(shù)據(jù)要加上什么限制,如果超出限制,要報告什么錯誤。
-
username(用戶名)
限制 錯誤提示 必填 請?zhí)顚?/td> 不能重復(fù) 用戶名已被人占用 只能使用英文字母、數(shù)字及下劃線 用戶名只允許使用英文字母、數(shù)字及下劃線 最短 3 位 用戶名最短 3 位 最長 15 位 用戶名最長 15 位 不能是 admin
或administrator
這種系統(tǒng)保留的用戶名系統(tǒng)保留,無法注冊,請更換 -
email(郵箱)
限制 錯誤提示 必填 請?zhí)顚?/td> 不能重復(fù) 郵箱已被人占用 郵箱必須包含 @
字符郵箱格式錯誤 -
password(密碼)
限制 錯誤提示 必填 請?zhí)顚?/td> 密碼最短 6 位 密碼最短 6 位 密碼不能明文存儲在數(shù)據(jù)庫中 -
好了,接下來準(zhǔn)備寫代碼。
樣板命令
如何添加的幫助頁面?
- 在
web/router.ex
文件中添加路由 - 添加控制器文件
help_controller.ex
- 添加視圖文件
help_view.ex
- 添加模板文件
index.html.eex
但這樣的手動添加過程太麻煩,還容易出錯,應(yīng)該有便捷的方法。
是的,Phoenix 提供了一系列的 mix 工具包。我們要接觸的這個是 mix phx.gen.html
。
請在命令行窗口下切換到 tv_recipe
目錄,然后執(zhí)行 mix phx.gen.html
命令:
$ cd tv_recipe
$ mix phx.gen.html Users User users username:string:unique email:string:unique password:string

執(zhí)行命令后的輸出如下:
* creating lib/tv_recipe_web/controllers/user_controller.ex
* creating lib/tv_recipe_web/templates/user/edit.html.eex
* creating lib/tv_recipe_web/templates/user/form.html.eex
* creating lib/tv_recipe_web/templates/user/index.html.eex
* creating lib/tv_recipe_web/templates/user/new.html.eex
* creating lib/tv_recipe_web/templates/user/show.html.eex
* creating lib/tv_recipe_web/views/user_view.ex
* creating test/lib/tv_recipe_web/controllers/user_controller_test.exs
* creating lib/tv_recipe/users/user.ex
* creating priv/repo/migrations/20170123145857_create_user.exs
* creating lib/tv_recipe/users.ex
* injecting lib/tv_recipe/users.ex
* creating test/lib/tv_recipe/users_test.exs
* injecting test/tv_recipe/users_test.exs
Add the resource to your browser scope in lib/tv_recipe_web/router.ex:
resources "/users", UserController
Remember to update your repository by running migrations:
$ mix ecto.migrate
命令生成的文件很多,我們來看最底下的兩段提示:
- 添加
resources "/users", UserController
到web/router.ex
文件中 - 命令行下執(zhí)行
mix ecto.migrate
前幾章里,我們在添加幫助頁面時,給 web/router.ex
文件添加過一行代碼:
get "/help", HelpController, :index
這里,resources "/users", UserController
起的是類似作用。我們可以不厭其煩地寫成如下:
get "/users", UserController, :index
get "/users/:id/edit", UserController, :edit
get "/users/new", UserController, :new
get "/users/:id", UserController, :show
post "/users", UserController, :create
patch "/users/:id", UserController, :update
put "/users/:id", UserController, :update
delete "/users/:id", UserController, :delete
可是,誰不會厭煩呢?所以 Phoenix 提供了 resources
這一便捷方法。
再來說說 mix ecto.migrate
。
目前為止,我們還沒有真正操作過數(shù)據(jù)庫。可我們的用戶數(shù)據(jù)必須存儲在數(shù)據(jù)庫中,我們難道要自己手動執(zhí)行 SQL 語句來創(chuàng)建用戶表格?
不不不,我們只要運(yùn)行 mix ecto.migrate
,一切便都妥當(dāng)了:
$ mix ecto.migrate
Compiling 15 files (.ex)
Generated tv_recipe app
11:08:12.056 [info] == Running TvRecipe.Repo.Migrations.CreateUser.change/0 forward
11:08:12.057 [info] create table users
11:08:12.065 [info] create index users_username_index
11:08:12.066 [info] create index users_email_index
11:08:12.067 [info] == Migrated in 0.0s
操作完上述兩步后,因?yàn)槟承┚庉嬈骺赡軐?dǎo)致的代碼重載問題,你需要重啟 Phoenix 服務(wù)器 - 按兩次 Ctrl-C,然后重新執(zhí)行 mix phx.server
。
之后在瀏覽器中打開網(wǎng)址 http://localhost:4000/users/new
:
有了。是不是很驚訝?我們用 mix phx.gen.html
命令生成的樣板,功能已經(jīng)很完善:增刪改查功能全都有了。我們需要的,只是在樣板基礎(chǔ)上做點(diǎn)修改。
更多建議: