Trouble with understanding routing priority

I am building some sort of discussion board, where you can ask questions and write answers. Now I ran into a problem with routing and seem to be understanding them wrong.

I have a homepage called index, from there you can click a button “Ask question” or another button “Write Answer”. Each button leads to another webpage (askQuestion.scala.html or writeAnswer.scala.html) where you can write a question or answer. After hitting submit you come back to the index-page, where the new question or answer is put into the view. In the background, the question / answer gets put into a DB.

My routes.conf:

# Home page
GET     /                           controllers.Application.index()

GET     /FrageStellen               controllers.Application.askQuestion()
POST    /                           controllers.Application.sendQuestion()

GET     /AntwortGeben               controllers.Application.writeAnswer()
POST    /                           controllers.Application.sendAnswer()

But when I enter an answer, it gets written into the question-table in the DB! This is due to the fact that the question-route is higher up in the routing table and therefore it seems to get selected first.

This is against my understanding of routes, I thought a route consists of a 3-tuple: HTTP-Method, Request Path, Call definition … and as the call definitions differ (sendQuestion() and sendAnswer()), the correct one should be used?

Why is this not the case?

I’ve read about routing in the documentation of the play framework and googled, but still dont understand.

I am also aware how to fix my problem, but I want to understand what’s happening here.

Fix 1:
Change this

POST    /                           controllers.Application.sendAnswer()

to this

POST    /Antwort                    controllers.Application.sendAnswer()

Disadvantage: This is not the homepage (index) anymore. Seems weird.

Fix 2:
Write a combined method for sending stuff from the form to the index.

Disadvantage: I want to keep the methods separate, in order to maintain a better structure in my project. Also I would have to see if a question gets asked or an answer written and based on that omit one of the fields or use another one (answer has an extra questionID-field to link an answer to a question).

So, why is this happening in the routes and whats the best way to deal with it?

Source: New Questions

Leave a Reply