1

私は現在、scala (2.11)、Playframework (2.2.3)、および MongoDB (Salat を使用) を使用して最初のアプリケーションを作成しています。私はそれらのどれもあまりよく知らないので、少し初心者の問題があります.フォームからデータを送信できません.

誰かが私の正確な問題で私を助けてくれるかどうかはよくわかりませんが、誰かが私を正しい方向に向けることができるでしょうか?

私の現在のエラーは次のとおりです。

[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)) {

これが私のコードです: 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>
    }
}

私のコントローラーの一部 - 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))

このリクエストのルート:

POST    /user               controllers.api.Users.create()

そして最後に私のモデル - 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 _)

Play for Scala (Manning) とそのトピックで見つけた Google リンクを利用して、https://github.com/leon/play-salatに基づいてさまざまなバリエーションを試しました。これはかなり明白な問題のように思えますが、私が言ったように、私はこれらのトピックに非常に慣れていません。誰かが私を正しい方向に向けたり、チュートリアルや何かを送ってくれたりしたら、それは素晴らしいことです! ありがとうございました!

4

0 に答える 0