Skip to content

Instantly share code, notes, and snippets.

@sam
Created October 27, 2014 18:32
Show Gist options
  • Save sam/437c90bfdc8674224492 to your computer and use it in GitHub Desktop.
Save sam/437c90bfdc8674224492 to your computer and use it in GitHub Desktop.
Have a reverse route match multiple Request Methods in Play Framework
object MyController extends Controller with StrictLogging {
def index =
AdminAction.async { implicit request =>
request.method match {
case "GET" =>
Future.successful(Ok("GET"))
case "POST" =>
def onError(errors: Form[FooCreateDirective]): Future[Result] = {
Future.successful(BadRequest("ERROR"))
}
def onSuccess(directive: FooCreateDirective): Future[Result] = {
operation(directive) map {
case Success(_) => Redirect(routes.MyController.index())
case Failure(e) =>
logger.error("MyController.createOnSuccess failed", e)
InternalServerError
}
}
fooCreateForm.bindFromRequest().fold(onError, onSuccess)
case _ => Future.successful(MethodNotAllowed(request.method))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment