概要
wsl(wsl2じゃない)で、elixirやってみた。
練習問題、やってみた。
練習問題
Phoenixで、postを受けるページを作れ。
方針
- formタグ使う。
- hello.html.heexを新規
- routerを書く
- controlerを書く
- csrf対策
写真
手順
- hello2/lib/hello2_web/templates/page/index.html.heexを修正。
<section class="phx-hero">
<h1><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h1>
<form action="/hello" method="post">
<input type="text" name="messenger">
<input type="hidden" name="_csrf_token" value={Phoenix.Controller.get_csrf_token()} >
<input type="submit" value="enter">
</form>
<p>Peace of mind from prototype to production</p>
</section>
- hello2/lib/hello2_web/router.exを修正。
post "/hello", PageController, :hello
- hello2/lib/hello2_web/contlorers/page_controler.exを修正。
def hello(conn, %{"messenger" => messenger}) do
render conn, "hello.html", messenger: messenger
end
- hello2/lib/hello2_web/templates/page/hello.html.heexを新規
<div class="jumbotron">
<h2>Welcome to Phoenix! Hi, <%= @messenger %></h2>
<p class="lead">A productive web framework that<br />does not compromise speed and maintainability.</p>
</div>
mix phx.routes
$ 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
page_path GET / Hello2Web.PageController :index
page_path POST /hello Hello2Web.PageController :hello
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
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
以上。