その2からのつづき。
画面の初期表示までできたので、あとは実際に画面を動かしてみる。
が、続きに入る前に、今の状態でCreate
ボタンを押すと以下のようなエラーになる。
Unauthorized
You must be authenticated to access this page.
ので、以下のような修正を行う。
Application.scala
- def tasks = Action {
+ def tasks = Action { implicit request =>
index.scala.html
-@(tasks: List[Task], taskForm: Form[String])(implicit messsages:Messages)
+@(tasks: List[Task], taskForm: Form[String])(implicit messsages:Messages, request: RequestHeader)
...
@form(routes.Application.newTask) {
+ @CSRF.formField
@inputText(taskForm("label"))
「フォーム投稿処理」の章
-
Application.scala
のnewTask
アクションのコードを実装する。
「データベース内のタスクを永続化する」の章
-
build.sbt
とconf/application.conf
にそれぞれ以下の設定を追記する。
build.sbt
libraryDependencies += evolutions
libraryDependencies ++= Seq(
jdbc,
"com.typesafe.play" %% "anorm" % "2.4.0"
)
application.conf
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
- コマンドプロンプトで
Ctrl+D
を押してサーバを停止させ、exit
コマンドでsbtを一旦抜ける。2回目以降の起動はsbt run
コマンド一発でok。 - チュートリアルに従ってエボリューションスクリプト
1.sql
を作成する。 - ブラウザをリロードし、
Apply this script now!
ボタンを押す。 - チュートリアルに従い、
Task.scala
のコードを実装する。
「タスクの削除」の章
-
Application.scala
のdeleteTask
アクションのコードを実装する。
これによりtodoリストの削除操作も行えるようになる。
と、思いきや、削除操作についても
Unauthorized
You must be authenticated to access this page.
になってしまうので、追加時と同様にコード修正を行う。
index.scala.html
@form(routes.Application.deleteTask(task.id)) {
+ @CSRF.formField
<input type="submit" value="Delete">
以上。