chotchki
chotchki

Reputation: 4343

In Play Scala Call a controller method from another method

I have the following controller defined in Play Scala 2.2.

object Blog extends Controller {
    val postForm = Form(
        mapping(
            "title" -> nonEmptyText,
            "content" -> nonEmptyText
            )(Post.apply)(Post.unapply)
    )

    def defaultList = Action {
        list(DateTime.now())
    }

    def list(date: DateTime) = Action {
      Ok(views.html.posts(Posts.all(), postForm))
    }
}

I'm getting the following error on compilation:

Overloaded method value [apply] cannot be applied to (play.api.mvc.Action[play.api.mvc.AnyContent])

Any ideas what I'm doing wrong? Can I call one controller method from another? I'm VERY new to Scala and Play.

Upvotes: 1

Views: 2407

Answers (1)

arussinov
arussinov

Reputation: 1237

You can't wrap in this way, because any action in play2 takes a function: Request => Result so from request to result, and in your code you return another Action, so it won't compile. You can do like @Akos Krivachy propose, put your call of list Action inside a function not an Action and then bind this function to some route for example. In this case you may have only one route to bind your defaultList function. Or you can leave your defaultList as an Action and use a redirect inside:

def defaultList = Action {
        Redirect(routes.Application.list(DateTime.now()))
    }

Note : you need to indicate routes for both Actions in route file

Upvotes: 1

Related Questions