1

如何在使用 Scala 的 Play 框架创建的待办事项应用程序中添加额外的字段?我正在使用异常数据库...我在第 24 行收到一个错误"not found: value Task"Application.scala我已经尝试过了,请指出我的错误。提前致谢!

task.scala

package models

import anorm._
import anorm.SqlParser._
import play.api.db._
import play.api.Play.current


case class Task(id: Long, label: String, name: String)

object Task {
  val task = {
    get[Long]("id") ~ 
    get[String]("label") ~ 
    get[String]("name") map {
      case label~name => Task(id, name)
      case id~label => Task(id, label)
    }
  }

  def all(): List[Task] = DB.withConnection { implicit c =>
    SQL("select * from task").as(task *)
  }
  def create(task: Task): Unit= {
    DB.withConnection { implicit c =>
      SQL("insert into task (label,name) values ({label},{name})").on(
        'label -> label,
        'name  -> name
      ).executeUpdate()
    }
  }

  def delete(id: Long) {
    DB.withConnection { implicit c =>
      SQL("delete from task where id = {id}").on(
        'id -> id
      ).executeUpdate()
    }
  }
}

application.scala(控制器类):

package controllers

import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._

import play.api.data.Form
import play.api.data.Forms.{tuple,nonEmptyText}
import play.api.mvc.{Action, Controller}
import anorm.NotAssigned

import models.Task

object Application extends Controller {
  def index = Action {
    Redirect(routes.Application.tasks)
  }
  val taskForm = Form(
    tuple(
      "label" -> nonEmptyText,
      "name" -> nonEmptyText
    )
  )

  def tasks = Action {
    Ok(views.html.index(Task.all(), taskForm))
  }
  def showTask= Action {
    Ok(views.html.test(Task.all(), taskForm))
  }

  def newTask = Action { implicit request =>
    taskForm.bindFromRequest.fold(
      errors => BadRequest(views.html.index(Task.all(), errors)),
      {
        case (label, name) => {
          Task.create(Task(NotAssigned, label, name))
          Redirect(routes.Application.showTask)
        }
      }
    )
  }

  def deleteTask(id: Long) = Action {
    Task.delete(id)
    Redirect(routes.Application.showTask)
  }
}

索引(查看文件):

@(tasks: List[Task], taskForm: Form[(String, String)])

@import helper._

<h2>Add a new task</h2>

@form(routes.Application.newTask) {

  @inputText(taskForm("label")) 
  @inputText(taskForm("name"))

  <input type="submit" value="Create">
}

test.html(查看文件 2):

@(tasks: List[Task], taskForm: Form[(String,String)])

@import helper._

@main("Todo list") {
  <h1>@tasks.size task(s)</h1>

  <ul>
    @tasks.map { task =>
      <li>
        <b>@task.label</b>
        <b>@task.name</b>

        @form(routes.Application.deleteTask(task.id)) {
          <input type="submit" value="Delete">
        }
      </li>
    }
  </ul>  
}
4

2 回答 2

1

尝试使用:

(申请)和(不申请)

表单元素的正确方法。

(Task.apply)(Task.unapply)
于 2013-10-24T06:42:44.517 回答
0

import models.Task._将伴生对象上的所有方法models.Task导入当前范围,而不是Task类和对象本身。因此,当前代码将允许您只调用all它会指Task.all

将导入更改为将import models.TaskTask 放入 Application 对象的范围内,您将能够像尝试那样使用任务方法。

于 2013-09-18T09:08:53.740 回答