java - Trouble with understanding routing priority -
i building sort of discussion board, can ask questions , write answers. ran problem routing , seem understanding them wrong.
i have homepage called index
, there can click button "ask question" or button "write answer". each button leads webpage (askquestion.scala.html or writeanswer.scala.html) can write question or answer. after hitting submit
come index-page, new question or answer put view. in background, question / answer gets put db.
my routes.conf:
# home page / controllers.application.index() #questions /fragestellen controllers.application.askquestion() post / controllers.application.sendquestion() #answers /antwortgeben controllers.application.writeanswer() post / controllers.application.sendanswer()
but when enter answer, gets written question-table in db! due fact question-route higher in routing table , therefore seems selected first.
this against understanding of routes, thought route consists of 3-tuple: http-method, request path, call definition
... , call definitions differ (sendquestion() , sendanswer()), correct 1 should used?
why not case?
i've read routing in documentation of play framework , googled, still dont understand.
i aware how fix problem, want understand what's happening here.
fix 1: change this
post / controllers.application.sendanswer()
to this
post /antwort controllers.application.sendanswer()
disadvantage: not homepage (index) anymore. seems weird.
fix 2: write combined method sending stuff form index.
disadvantage: want keep methods separate, in order maintain better structure in project. have see if question gets asked or answer written , based on omit 1 of fields or use 1 (answer has questionid-field link answer question).
so, why happening in routes , whats best way deal it?
in play each route combination of route method (get
/post
) , path (determined static parts , params types) if have 2 routes same type , path only first resolvable, other ignored if you'll use other name of param (but same param type).
in case bar(string bar)
method won't resolved ever:
get /foo/:foo controllers.application.foo(foo: string) /foo/:bar controllers.application.bar(bar: string)
the safest way make sure won't mishmash routes using unique paths in sets:
get / controllers.application.index() /fragestellen controllers.application.askquestion() post /fragestellen controllers.application.sendquestion() /antwortgeben controllers.application.writeanswer() post /antwortgeben controllers.application.sendanswer()
finally if want go root page after post can return redirect()
instead of ok()
Comments
Post a Comment