如何在待办事项应用程序中添加额外的字段

时间:2013-09-18 07:16:48

标签: scala playframework-2.0 anorm

如何在使用Scala使用Play框架创建的待办事项应用程序中添加额外字段?我正在使用anorm DB ...我在第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>  
}

2 个答案:

答案 0 :(得分:1)

尝试使用:

(申请)和(取消申请)

方法适用于表单元素。

(Task.apply)(Task.unapply)

答案 1 :(得分:0)

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

将导入更改为import models.Task,而不是将Task放入Application对象的作用域中,您将能够像使用的那样使用任务方法。