Scala - Cannot use a method returning play.api.mvc.Result as a Handler for requests
Asked Answered
I

1

9

I have this controller in Scala:

def commonRedirect(anId: Long) = {
implicit val aRule = CommonClient.getTheRule(anId)
aRule match {
  case false ⇒ Redirect("/general-rule/" + anId)
  case true  ⇒ Redirect("/custom-rule/" + anId)
}

}

but, this result in the error: "Cannot use a method returning play.api.mvc.Result as a Handler for requests".

If I apply an Action Builder, it works, but this is not the way that I want.

Any ideas to resolve this?

Thanks.

Inarch answered 18/2, 2016 at 13:42 Comment(1)
Can you specify how you want to use commonRedirect. I have no errors with your code.Everhart
G
14

You need to make an Action.

def commonRedirect(anId: Long) = Action {
  implicit val aRule = CommonClient.getTheRule(anId)
  aRule match {
    case false ⇒ Redirect("/general-rule/" + anId)
    case true  ⇒ Redirect("/custom-rule/" + anId)
  }
}
Geophilous answered 18/2, 2016 at 15:45 Comment(1)
Thanks @paul-draper. I found in https://mcmap.net/q/796707/-what-does-the-word-quot-action-quot-do-in-a-scala-function-definition-using-the-play-framework that the required type for that is not just Result but ⇒ Result, and that is an Action.Inarch

© 2022 - 2024 — McMap. All rights reserved.