Created
April 9, 2012 16:20
-
-
Save julienrf/2344517 to your computer and use it in GitHub Desktop.
How to implement a custom PathBindable with Play 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def show(article: Article) = Action { | |
Ok(views.html.article(article)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Article(id: Long, name: String, price: Double) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit def articlePathBindable(implicit longBinder: PathBindable[Long]) = new PathBindable[Article] { | |
def bind(key: String, value: String): Either[String, Article] = | |
for { | |
id <- longBinder.bind(key, value).right | |
article <- Article.findById(id).toRight("Article not found").right | |
} yield article | |
def unbind(key: String, article: Article): String = | |
longBinder.unbind(key, article.id) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings( | |
routesImport += "binders._" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override def onBadRequest(request: RequestHeader, error: String) = error match { | |
case "Article not found" => Redirect(controllers.routes.Articles.list) | |
case _ => super.onBadRequest(request, error) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET /show/:article controllers.Articles.show(article: models.Article) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, could someone help with this question? https://stackoverflow.com/questions/66404308/play-framework-cannot-find-querystringbinders