So in Play 2.0 I had this:
GET /tasks/add controllers.Tasks.addTask(parentId: Option[Long] = None)
GET /tasks/:parentId/add controllers.Tasks.addTask(parentId: Option[Long])
With a controller method like this:
def addTask(parentId: Option[Long]) = Action {
Ok(views.html.addTask(taskForm, parentId))
}
And it was working. As I migrated to 2.1, it seems to complain about these lines with: No URL path binder found for type Option[Long]. Try to implement an implicit PathBindable for this type.
Basically, what I am trying to accomplish is to have route tasks/add
and route tasks/123/add
link to the same method that accepts an Optional[Long]
. Any idea how to do this? Thanks.
Ok, so I got a kind of it's not a bug, it's a feature response on Lighthouse: "We removed Option[Long] support in path bindables since it doesn't make sense to have a optional path parameter. You can implement your own path bindable that supports it if you please." So far I have 2 solutions, passing -1 as parentId, which I do not really like. Or having 2 different methods, which probably makes more sense in this case. Implementing the PathBindable doesn't seem too feasible right now, so I will probably stick with having 2 methods.
From this routes-with-optional-parameter the suggestion goes like:
The simple solution to your problem, without having to pass a default value, is to add a simple proxy method that wraps the parameter in an option.
Routes:
Controller:
I had the same thing and more if you specify the pass as
GET/foo:id
andcontrollers.Application.foo(id : Option[Long] ?= None)
you get an errorIt is not allowed to specify a fixed or default value for parameter: 'id' extracted from the path
on the other side you can do as followsGET/foo controllers.Application.foo(id : Option[Long] ?= None)
and it will work expecting that your request looks like as.../foo?id=1
I think you have to add a question mark:
controllers.Tasks.addTask(parentId: Option[Long] ?= None)
Play 2.0 supported
Option
in path parameters, Play 2.1 no longer supports this, they removed the PathBindable for Option.One possible solution would be:
And add this to
Build.scala
usingroutesImport += "extensions.Binders._"
. Runplay clean ~run
and it should work. Reloading the Binders on the fly only sometimes works.