Rodrigo Sasaki
Rodrigo Sasaki

Reputation: 7226

What is wrong with this PathBindable?

I'm trying to send an object of my model as a parameter on a GET of PlayFramework

I created the case class Game, that looks something like this:

case class Game(name: String, url: String){

}

And when I tried to send it as a parameter to my method like this (routes file):

GET     /trophies/:game             controllers.Application.trophies(game: model.Game)

It told me I needed a PathBindable object, so I did some research and came with an implementation I thought was valid:

case class Game(name: String, url: String) implements PathBindable[Game] {
    def bind(key: String, value: String): Game = {
        val text = value.split(";")
        Game(text(0), text(1))
    }

    def unbind(key: String, game: Game): String = {
        game.name + ";" + game.url
    }

    def javascriptUnbind(): String = ??? 
    def unbind(x$1: String): String = ???
}

To bind it I create a Game from the string divided with the ; and to unbind it I just create the divided String.

When I use it I get a Bad Request, stating that the Action was not found, but it exists, as I displayed on the routes file snippet above.

Is there something wrong with my implementation?

Upvotes: 1

Views: 340

Answers (1)

Schleichardt
Schleichardt

Reputation: 7552

It looks like you used the PathBindable from the Java API and missed the noarg constructor. but you need the Scala API.

In addition make sure that your game path representation is URL encoded (it should not contain raw slashes "/") or would not match. But you can bind an URL part with /trophies/*game so that it matches simply the whole remaining URL after trophies as documented here.

Upvotes: 1

Related Questions