使用Scala和Play Framework

时间:2015-07-16 13:15:00

标签: scala anorm playframework-2.4

我跟着this application tutorial。基本上,在这个应用程序中,他们创建了一个待办事项。我已将模型复制到调查应用程序中,我想创建一个包含问题文本和问题类型(二进制文本,文本条目等)的问题。关于我的问题表格格式等,我得到了不同程度的错误,但主要问题似乎是newQuestion行动

questionForm.bindFromRequest.fold(errors=> ..., question=> ...)

我的代码格式如下:

QuestionController.scala:

def questions = Action {
  Ok(views.html.question(Question.all(), questionForm))
}

def newQuestion = Action { implicit request =>
  questionForm.bindFromRequest.fold(
    errors => BadRequest(views.html.question(Question.all(), errors)),
      question => {     
        Question.create(question)
        Redirect(routes.QuestionController.questions)      
    }
  )
}

def deleteQuestion(id: Long) = Action {
  Question.delete(id)
  Redirect(routes.QuestionController.questions)
}

val questionForm = Form(
  "questiontext" -> nonEmptyText    
)

CaseClass / companion对象Question.scala:

case class Question (id:Long, questionText:String, questionType:String)

object Question {
  val question = {
    get[Long]("id") ~
      get[String]("questiontext") ~
      get[String]("questiontype") map {
      case id~questiontext~questiontype => Question(id, questiontext,questiontype)
    }
  }

  def all(): List[Question] = DB.withConnection { implicit c =>
    SQL("select * from questions").as(question *)
  }

  def create(text:String) {
    DB.withConnection {
      implicit c =>
        SQL("insert into questions(questiontext) values ({text})").on(
          'text -> text
        //'quesType -> qType
        ).executeUpdate()
    }
  }

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

我基本上想要使用两个参数运行create:

def create(text:String,qType:String) {
  DB.withConnection { implicit c =>
    SQL("insert into questions(questiontext,questiontype) values ({text},{quesType})").on(
      'text -> text,
      'quesType -> qType
    ).executeUpdate()
  }
}

但我不确定如何在前面提到的bindFromRequest.fold处理这个问题。

  • 我尝试编辑questionForm以使用映射并使用(Question.apply)(Question.unapply)以及元组以及该元组中包含的questiontext -> nonEmptyTextquestionType -> nonEmptyText而没有运气。
  • 我编辑了scala.html文件以相应地包含正确的值类型但仍然没有运气。
  • 我可以使用一列来保存它,但它崩溃了,因为我将questionType定义为pgsql中的必填字段,并且在没有问题类型的情况下附加后~get questionType from *上的get失败。< / LI>

更新:

我已对其进行了编辑,以尝试在此处模拟代码: https://www.playframework.com/documentation/2.4.1/ScalaForms

现在,我使用以下代码:

  def questions = Action {
    Ok(views.html.question(Question.all(), questionForm))
  }


  def newQuestion = Action { implicit request =>
    logger.error(s"wtf")
    questionForm.bindFromRequest.fold(
      errors => {BadRequest(views.html.question(Question.all(), errors))
      },
      question => {
            Question.create(question:Question)
            Redirect(routes.QuestionController.questions).flashing("success"->"Question saved successfully!")
      }
    )
  }

  def deleteQuestion(id: Long) = Action {
    Question.delete(id)
    Redirect(routes.QuestionController.questions)
  }

  val questionForm:Form[Question]=Form(mapping(
  "id" -> longNumber,
  "versionid" -> longNumber,
    "questiontext" -> nonEmptyText,
  "questiontype" -> nonEmptyText)(Question.apply)(Question.unapply)

  )

我已更新了我的question.scala.html文件,以@(questions: List[Question], questionForm: Form[(Question)])作为输入。

当我点击提交时,我不断收到以下消息:wtf

按照我最近链接的示例,我不明白为什么它不会转到question的情况并继续在数据库中创建问题。我更新了我的数据库创建函数,如下所示:

  def create(question:Question) {
    DB.withConnection {
      implicit c =>
        SQL("insert into questions(questiontext,questiontype,versionid) values ({text},{quesType},{versId})").on(
          'text -> question.questionText,
        'quesType -> question.questionType,
        'versId -> 1.toLong
        ).executeUpdate()
    }
  }

感谢迄今为止的帮助,我希望我能在短时间内弄明白我的错误。

0 个答案:

没有答案
相关问题