Chapter 03

ScaffoldでCRUDを自動生成する

toyoshi
toyoshi
2021.12.30に更新

mix phx.gen.html にコンテキスト名とスキーマの単数形と複数形、スキーマのカラムを指定することで生成できます。(今回はコンテキスト名とスキーマ名を同じにしています)

% mix phx.gen.html Articles Article articles title:string body:text
* creating lib/blog_web/controllers/article_controller.ex
* creating lib/blog_web/templates/article/edit.html.heex
* creating lib/blog_web/templates/article/form.html.heex
* creating lib/blog_web/templates/article/index.html.heex
* creating lib/blog_web/templates/article/new.html.heex
* creating lib/blog_web/templates/article/show.html.heex
* creating lib/blog_web/views/article_view.ex
* creating test/blog_web/controllers/article_controller_test.exs
* creating lib/blog/articles/article.ex
* creating priv/repo/migrations/20211230120238_create_articles.exs
* creating lib/blog/articles.ex
* injecting lib/blog/articles.ex
* creating test/blog/articles_test.exs
* injecting test/blog/articles_test.exs
* creating test/support/fixtures/articles_fixtures.ex
* injecting test/support/fixtures/articles_fixtures.ex

Add the resource to your browser scope in lib/blog_web/router.ex:

    resources "/articles", ArticleController


Remember to update your repository by running migrations:

    $ mix ecto.migrate