Created
March 10, 2012 23:35
-
-
Save danieldietrich/2014006 to your computer and use it in GitHub Desktop.
Play w/ Scala: Url Path Binding
This file contains hidden or 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
package models | |
import play.api.mvc.PathBindable | |
object Account extends Enumeration { | |
val Company, Sales = Value // create enumerated values | |
// url path binding (routes) | |
implicit def bindableAccount = new PathBindable[Account.Value] { | |
def bind(key: String, value: String) = | |
Account.values.find(_.toString.toLowerCase == value.toLowerCase) match { | |
case Some(v) => Right(v) | |
case None => Left("Unknown account type '" + value + "'") | |
} | |
def unbind(key: String, value: Account.Value) = value.toString | |
} | |
} |
This file contains hidden or 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
@(account: models.Account.Value) | |
@main() { | |
<p>Look ma, bound url path to @account</p> | |
} |
This file contains hidden or 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
# Signup | |
GET /signup/:account controllers.SignUp.form(account: models.Account.Value) |
This file contains hidden or 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
package controllers | |
import models._, play.api.mvc._, views._ | |
object SignUp extends Controller { | |
def form(account: Account.Value) = Action { | |
Ok(html.signup.form(account)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment