Skip to content

Instantly share code, notes, and snippets.

@erikrozendaal
Created February 18, 2012 14:21
Show Gist options
  • Save erikrozendaal/1859530 to your computer and use it in GitHub Desktop.
Save erikrozendaal/1859530 to your computer and use it in GitHub Desktop.
Play! 2.0
# --- !Ups
create table users (
name text primary key,
age int not null);
# --- !Downs
drop table users;
def page(name: String, format: String) = Action {
Ok(<p>You requested page {name} in format {format}</p>.toString).as(HTML)
}
import play.api.data._, Forms._, validation.Constraints._
// Domain object
case class User(name: String, age: Int)
// Form definition
val userForm = Form(
mapping(
"name" -> nonEmptyText,
"age" -> number(min = 0, max = 150)
)(User.apply)(User.unapply))
// Form submit action
def register = Action { implicit request =>
userForm.bindFromRequest.fold(
errors => BadRequest(views.html.index(errors)),
user => Redirect(routes.Application.index))
}
@(registrationForm: Form[User])
@helper.form(action = routes.Application.register) {
@helper.inputText(registrationForm("name"))
@helper.inputText(registrationForm("age"))
<input type="submit" value="Register" />
}
@(message: String)
@main("Welcome to Play 2.0") {
<a href="@routes.Application.page("1984", "PDF")">page example</a>
}
import play.api.db._, anorm._, play.api.Play.current
def registeredUsers = DB.withConnection { implicit connection =>
SQL("select name, age from users")().map(row => User(row[String]("name"), row[Int]("age"))).toList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment