概要
wsl(wsl2じゃない)で、elixirやってみた。
練習問題、やってみた。
練習問題
Phoenixで、LiveViewを実装せよ。
方針
- phx.gen.live 使う。
- コンテキストモジュール Blogs
- スキーマモジュール Blog
- スキーマ モジュールの複数形名 blogs
- 名前と型 title:string body:text
写真
手順
- mix phx.gen.live を実行
$ mix phx.gen.live Blogs Blog blogs title:string body:text
* creating lib/hello2_web/live/blog_live/show.ex
* creating lib/hello2_web/live/blog_live/index.ex
* creating lib/hello2_web/live/blog_live/form_component.ex
* creating lib/hello2_web/live/blog_live/form_component.html.heex
* creating lib/hello2_web/live/blog_live/index.html.heex
* creating lib/hello2_web/live/blog_live/show.html.heex
* creating test/hello2_web/live/blog_live_test.exs
* creating lib/hello2_web/live/live_helpers.ex
* creating lib/hello2/blogs/blog.ex
* creating priv/repo/migrations/20230915212012_create_blogs.exs
* creating lib/hello2/blogs.ex
* injecting lib/hello2/blogs.ex
* creating test/hello2/blogs_test.exs
* injecting test/hello2/blogs_test.exs
* creating test/support/fixtures/blogs_fixtures.ex
* injecting test/support/fixtures/blogs_fixtures.ex
* injecting lib/hello2_web.ex
Add the live routes to your browser scope in lib/hello2_web/router.ex:
live "/blogs", BlogLive.Index, :index
live "/blogs/new", BlogLive.Index, :new
live "/blogs/:id/edit", BlogLive.Index, :edit
live "/blogs/:id", BlogLive.Show, :show
live "/blogs/:id/show/edit", BlogLive.Show, :edit
Remember to update your repository by running migrations:
$ mix ecto.migrate
- router.exを修正
live "/blogs", BlogLive.Index, :index
live "/blogs/new", BlogLive.Index, :new
live "/blogs/:id/edit", BlogLive.Index, :edit
live "/blogs/:id", BlogLive.Show, :show
live "/blogs/:id/show/edit", BlogLive.Show, :edit
- mix ecto.migrateを実行
$ mix ecto.migrate
warning: the :gettext compiler is no longer required in your mix.exs.
Please find the following line in your mix.exs and remove the :gettext entry:
compilers: [..., :gettext, ...] ++ Mix.compilers(),
(gettext 0.23.1) lib/mix/tasks/compile.gettext.ex:5: Mix.Tasks.Compile.Gettext.run/1
(mix 1.13.4) lib/mix/task.ex:397: anonymous fn/3 in Mix.Task.run_task/3
(mix 1.13.4) lib/mix/tasks/compile.all.ex:92: Mix.Tasks.Compile.All.run_compiler/2
(mix 1.13.4) lib/mix/tasks/compile.all.ex:72: Mix.Tasks.Compile.All.compile/4
(mix 1.13.4) lib/mix/tasks/compile.all.ex:59: Mix.Tasks.Compile.All.with_logger_app/2
(mix 1.13.4) lib/mix/tasks/compile.all.ex:36: Mix.Tasks.Compile.All.run/1
06:27:21.829 [info] == Running 20230915212012 Hello2.Repo.Migrations.CreateBlogs.change/0 forward
06:27:21.839 [info] create table blogs
06:27:21.893 [info] == Migrated 20230915212012 in 0.0s
- routeを確認
$ mix phx.routes
warning: the :gettext compiler is no longer required in your mix.exs.
Please find the following line in your mix.exs and remove the :gettext entry:
compilers: [..., :gettext, ...] ++ Mix.compilers(),
(gettext 0.23.1) lib/mix/tasks/compile.gettext.ex:5: Mix.Tasks.Compile.Gettext.run/1
(mix 1.13.4) lib/mix/task.ex:397: anonymous fn/3 in Mix.Task.run_task/3
(mix 1.13.4) lib/mix/tasks/compile.all.ex:92: Mix.Tasks.Compile.All.run_compiler/2
(mix 1.13.4) lib/mix/tasks/compile.all.ex:72: Mix.Tasks.Compile.All.compile/4
(mix 1.13.4) lib/mix/tasks/compile.all.ex:59: Mix.Tasks.Compile.All.with_logger_app/2
(mix 1.13.4) lib/mix/tasks/compile.all.ex:36: Mix.Tasks.Compile.All.run/1
Compiling 1 file (.ex)
page_path GET / Hello2Web.PageController :index
page_path POST /hello Hello2Web.PageController :hello
page_path POST /upload Hello2Web.PageController :upload
todo_path GET /todos Hello2Web.TodoController :index
todo_path GET /todos/:id/edit Hello2Web.TodoController :edit
todo_path GET /todos/new Hello2Web.TodoController :new
todo_path GET /todos/:id Hello2Web.TodoController :show
todo_path POST /todos Hello2Web.TodoController :create
todo_path PATCH /todos/:id Hello2Web.TodoController :update
PUT /todos/:id Hello2Web.TodoController :update
todo_path DELETE /todos/:id Hello2Web.TodoController :delete
blog_index_path GET /blogs Hello2Web.BlogLive.Index :index
blog_index_path GET /blogs/new Hello2Web.BlogLive.Index :new
blog_index_path GET /blogs/:id/edit Hello2Web.BlogLive.Index :edit
blog_show_path GET /blogs/:id Hello2Web.BlogLive.Show :show
blog_show_path GET /blogs/:id/show/edit Hello2Web.BlogLive.Show :edit
url_path GET /api/urls Hello2Web.UrlController :index
url_path GET /api/urls/:id Hello2Web.UrlController :show
url_path POST /api/urls Hello2Web.UrlController :create
url_path PATCH /api/urls/:id Hello2Web.UrlController :update
PUT /api/urls/:id Hello2Web.UrlController :update
url_path DELETE /api/urls/:id Hello2Web.UrlController :delete
url_path OPTIONS /api/urls Hello2Web.UrlController :options
url_path OPTIONS /api/urls/:id Hello2Web.UrlController :options
live_dashboard_path GET /dashboard Phoenix.LiveDashboard.PageLive :home
live_dashboard_path GET /dashboard/:page Phoenix.LiveDashboard.PageLive :page
live_dashboard_path GET /dashboard/:node/:page Phoenix.LiveDashboard.PageLive :page
* /dev/mailbox Plug.Swoosh.MailboxPreview []
websocket WS /live/websocket Phoenix.LiveView.Socket
longpoll GET /live/longpoll Phoenix.LiveView.Socket
longpoll POST /live/longpoll Phoenix.LiveView.Socket
以上。