我已经按照这个应用程序教程。基本上,在这个应用程序中,他们创建了一个待办事项。我已将该模型复制到调查应用程序中,并且我想创建一个包含问题文本和问题类型(二进制、文本输入等)的问题。关于我的问题表格格式等,我得到了不同程度的错误,但主要问题似乎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 -> nonEmptyText
和questionType -> nonEmptyText
包含在该元组中的和没有运气。 - 我相应地编辑了
scala.html
文件以包含正确类型的值,但仍然没有运气。 - 我可以用一列保存它,但它崩溃了,因为我
questionType
在 pgsql 中将它定义为必填字段,并且在~get questionType from *
没有问题类型的情况下附加后获取失败。
更新:
我已经对其进行了编辑以尝试在此处模拟代码: 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()
}
}
感谢您迄今为止的帮助,我希望我能尽快找出我做错了什么。