LoginSignup
5
4

More than 5 years have passed since last update.

Scala-PlayFramework2.5で2.2時代のtodolistチュートリアルをやってみた・その3

Last updated at Posted at 2017-05-15

その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.scalanewTaskアクションのコードを実装する。

データベース内のタスクを永続化する」の章

  • build.sbtconf/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のコードを実装する。

これでtodoリストの新規追加が行えるようになる。
finish.PNG

タスクの削除」の章

  • Application.scaladeleteTaskアクションのコードを実装する。

これにより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">

以上。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4