I have 2 pages, one is for entering questions and one is for entering answers. Therefore 2 forms are used, which send their content (question or answer) to the index page. I also put the newly created question / answer into my database.
But strangely everything that I enter goes into the question table. Is this due to a typo somewhere or is bindFromRequest not working as intended?
Controller class application.java:
static List<Question> questionListAll = new ArrayList<Question>();
static List<Answer> answerListAll = new ArrayList<Answer>();
private static final Form<Question> newQuestionForm = Form.form(Question.class);
private static final Form<Answer> newAnswerForm = Form.form(Answer.class);
// Go to the ask question page
public static Result askQuestion(){
List<Question> questionHelper = new ArrayList<Question>();
for (Question questionItem : Question.find.all()) {
questionHelper.add(questionItem);
}
return ok(views.html.frageAntwort.render(newQuestionForm, questionHelper));
}
// Send the question to the indexpage
public static Result sendQuestion(){
// Create new question-form and fill it with the values from the other page
Form<Question> boundQuestion = newQuestionForm.bindFromRequest();
Question newQuestion = boundQuestion.get();
Question.create(newQuestion);
questionListAll.add(newQuestion);
return ok(views.html.index.render(questionListAll, answerListAll));
}
// Write an answer, goto answerpage
public static Result writeAnswer(){
List<Answer> answerHelper = new ArrayList<Answer>();
for (Answer answerItem : Answer.find.all()) {
answerHelper.add(answerItem);
}
return ok(views.html.antwortGeben.render(newAnswerForm, answerHelper));
}
// Send answer to indexpage
public static Result sendAnswer(){
Form<Answer> boundAnswer = newAnswerForm.bindFromRequest();
Answer newAnswer = boundAnswer.get();
Answer.create(newAnswer);
answerListAll.add(newAnswer);
return ok(views.html.index.render(questionListAll, answerListAll));
}
My antwortGeben.scala.html view class, where you can enter a new answer:
@import models.Question
@import models.Answer
@import helper._
@import helper.twitterBootstrap._
@(answerForm: Form[Answer], answerList: List[Answer])
@main("Antwort geben"){
@helper.form(action = routes.Application.sendAnswer()){
<fieldset>
@helper.inputText(answerForm("answerID"))
@helper.inputText(answerForm("questionID"))
@helper.inputText(answerForm("answerText"))
@helper.inputText(answerForm("voteScore"))
@helper.inputText(answerForm("userID"))
</fieldset>
<input type="submit" class="btn btn-default">
}
}
My frageAntwort.scala.html view class, where you can enter new questions:
@import models.Question
@import models.Answer
@import helper._
@import helper.twitterBootstrap._
@(questionForm: Form[Question], questionList: List[Question])
@main("Frage stellen"){
@helper.form(action = routes.Application.sendQuestion()){
<fieldset>
@helper.inputText(questionForm("questionID"))
@helper.inputText(questionForm("questionText"))
@helper.inputText(questionForm("voteScore"))
@helper.inputText(questionForm("userID"))
</fieldset>
<input type="submit" class="btn btn-default">
}
}
My routes.conf:
# Home page
GET / controllers.Application.index()
#Questions
GET /FrageStellen controllers.Application.askQuestion()
POST / controllers.Application.sendQuestion()
#Answers
GET /AntwortGeben controllers.Application.writeAnswer()
POST / controllers.Application.sendAnswer()
So when I go to the page where you can enter a new Answer, I type into the form the answerID, ... and when I click the button, every input goes into the question-table in my DB.
I already have googled for a solution. I also did activator clean ... activator compile ... activator run
and cleaned in my Scala IDE (Eclipse).
Using play framework 2.3.8 and Scala IDE 4.0.0.
Why does every input go into my question table in the DB?