Saturday, 15 June 2013

playframework 2.0 - Routes with optional parameter - Play 2.1 Scala -



playframework 2.0 - Routes with optional parameter - Play 2.1 Scala -

so in play 2.0 had this:

get /tasks/add controllers.tasks.addtask(parentid: option[long] = none) /tasks/:parentid/add controllers.tasks.addtask(parentid: option[long])

with controller method this:

def addtask(parentid: option[long]) = action { ok(views.html.addtask(taskform, parentid)) }

and working. migrated 2.1, seems complain these lines with: no url path binder found type option[long]. seek implement implicit pathbindable type. basically, trying accomplish have route tasks/add , route tasks/123/add link same method accepts optional[long]. thought how this? thanks.

ok, got kind of it's not bug, it's feature response on lighthouse: "we removed option[long] back upwards in path bindables since doesn't create sense have optional path parameter. can implement own path bindable supports if please." far have 2 solutions, passing -1 parentid, not like. or having 2 different methods, makes more sense in case. implementing pathbindable doesn't seem feasible right now, stick having 2 methods.

play 2.0 supported option in path parameters, play 2.1 no longer supports this, removed pathbindable option.

one possible solution be:

package extensions import play.api.mvc._ object binders { implicit def optionbindable[t : pathbindable] = new pathbindable[option[t]] { def bind(key: string, value: string): either[string, option[t]] = implicitly[pathbindable[t]]. bind(key, value). fold( left => left(left), right => right(some(right)) ) def unbind(key: string, value: option[t]): string = value map (_.tostring) getorelse "" } }

and add together build.scala using routesimport += "extensions.binders._". run play clean ~run , should work. reloading binders on fly works.

scala playframework-2.0 playframework-2.1

No comments:

Post a Comment