I am currently writing my first application with scala (2.11), Playframework (2.2.3) and MongoDB (with Salat). Since I dont really know any of them too well I've got a bit of a beginner problem: I can't submit data from a form.
I am not really sure if anyone can help me with my precise problem, but maybe someone can point me in the right direction?
my current error is:
[error] .../app/controllers/api/Users.scala:96: Cannot find Formatter type class for se.radley.plugin.salat.Binders.ObjectId. Perhaps you will need to import play.api.data.format.Formats._
[error] "id" -> of[ObjectId],
[error] ^
[error] .../app/views/user.scala.html:7: type mismatch;
[error] found : play.api.mvc.EssentialAction
[error] required: String
[error] @helper.form(action = new play.api.mvc.Call("POST",api.Users.create)) {
Here's my code: view - users.scala.html:
@(userForm: Form[User])
@import views.html.helper._
@main("create new user") {
@helper.form(action = new play.api.mvc.Call("POST",api.Users.create)) {
@helper.inputText(userForm("username"))
<!-- other fields -->
<button type="submit">Create</button>
}
}
parts of my controller - Users.scala:
def create = JsonAction[SUser] { user =>
SUser.save(user, WriteConcern.Safe)
Ok(views.html.user(userForm)),
def createForm() = Action {
Ok(views.html.user(userForm))
}
val userForm = Form(mapping(
"id" -> of[ObjectId],
"username" -> nonEmptyText,
// other fields
)(SUser.apply)(SUser.unapply))
my route for this request:
POST /user controllers.api.Users.create()
and finally my model - User.scala:
case class User(
id: ObjectId = new ObjectId,
username: String,
// etc
object User extends UserDAO with UserJson
trait UserDAO extends ModelCompanion[User, ObjectId] {
def collection = mongoCollection("users")
val dao = new SalatDAO[User, ObjectId](collection) {} /* other methods */ }
trait UserJson {
implicit val userJsonWrite = new Writes[User] {
def writes(u: User): JsValue = {
Json.obj(
"id" -> u.id,
"username" -> u.username,
/* etc */ )}}
implicit val userJsonRead = (
(__ \ 'id).read[ObjectId] ~
(__ \ 'username).read[String] ~
/* etc */ ) (User.apply _)
I tried lots of different variations based on https://github.com/leon/play-salat with the help of Play for Scala (Manning) and google links I found on that topic. It seems to me this is a pretty obvious problem, but as I said I am very new to these topics. If anyone can point me at a right direction or even send me a tutorial or something it would be great! Thank you!