anorm - Insert item in DB with two columns using Scala and Play Framework -


i've followed this application tutorial. basically, in app create todo item. i've replicated model survey application, , want create question contains both question text , question type (binary, text entry etc). various degrees of errors regarding question form formatting, etc main problem seems in newquestion action on

questionform.bindfromrequest.fold(errors=> ..., question=> ...) 

my code formatted follows:

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 object 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 * questions").as(question *)   }    def create(text:string) {     db.withconnection {       implicit c =>         sql("insert questions(questiontext) values ({text})").on(           'text -> text         //'questype -> qtype         ).executeupdate()     }   }    def delete(id:long) {     db.withconnection{ implicit c =>         sql("delete questions id = {id}").on(           'id -> id         ).executeupdate()     }   } } 

i want run create 2 parameters such:

def create(text:string,qtype:string) {   db.withconnection { implicit c =>     sql("insert questions(questiontext,questiontype) values ({text},{questype})").on(       'text -> text,       'questype -> qtype     ).executeupdate()   } } 

but i'm unsure how handle in bindfromrequest.fold mentioned previously.

  • i tried editing questionform use mapping , using (question.apply)(question.unapply) tuple , questiontext -> nonemptytext , questiontype -> nonemptytext contained in tuple no luck.
  • i edited scala.html file contain right type of value accordingly still no luck.
  • i can save 1 column crashes because defined questiontype required field in pgsql , fails on ~get questiontype * after appending without question type.

update:

i've edited try , emulate code here: https://www.playframework.com/documentation/2.4.1/scalaforms

so now, using following code:

  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)    ) 

i've updated question.scala.html file take @(questions: list[question], questionform: form[(question)]) inputs.

when hit submit, keep getting log message of :wtf

following example linked, not understanding why not go case of question , continue create question in database. updated database create function following well:

  def create(question:question) {     db.withconnection {       implicit c =>         sql("insert questions(questiontext,questiontype,versionid) values ({text},{questype},{versid})").on(           'text -> question.questiontext,         'questype -> question.questiontype,         'versid -> 1.tolong         ).executeupdate()     }   } 

thanks far, hope can figure out doing wrong shortly.


Comments