2012-08-12 44 views
0

我正在使用Play-salat插件和我正在制作的应用程序! 2.0 scala。解决方案DuplicateKey:E11000与Play重复键错误索引! 2.0框架

我得到的重复键错误,编译点这里的错误:

object Post extends ModelCompanion[Post, ObjectId] 

我已阅读,这可以固定在PHP here做未设置的,请告诉我如何与做到这一点玩。

我能够在我的数据库中张贴一次,当我再次尝试时,我得到的错误。

def save = Action { implicit request => 
postForm.bindFromRequest.fold(
    formWithErrors => BadRequest(html.newpost(formWithErrors)), 
    post => { 
    Post.insert(post) 
    Home 
    } 
) 
} 

val postForm = Form(
mapping(
    "_id" -> ignored(new ObjectId()), 
    "title" -> nonEmptyText, 
    "content" -> nonEmptyText, 
    "posted" -> date("yyyy-MM-dd"), 
    "modified" -> optional(date("yyyy-MM-dd")), 
    "tags" -> nonEmptyText, 
    "categories" -> nonEmptyText, 
    "userId" -> nonEmptyText 
)(Post.apply)(Post.unapply) 
) 

回答

0

我自己解决了这个问题。我对Scala和Play框架很新颖。

我改变了我的Application.scala文件是这样的:

def newpost = Action { 
Ok(views.html.newpost(postForm)) 
} 

def save = Action { implicit request => 
postForm.bindFromRequest.fold(
    formWithErrors => BadRequest(html.newpost(formWithErrors)), 
    post => { 
    Post.create(post.title, post.content, post.posted, post.modified, post.categories, post.tags, post.author) 
    Home.flashing("success" -> "Post %s has been created".format(post.title)) 
    } 
) 
} 

val postForm = Form(
mapping(
    "_id" -> ignored(new ObjectId()), 
    "title" -> nonEmptyText, 
    "content" -> nonEmptyText, 
    "posted" -> date("yyyy-MM-dd"), 
    "modified" -> optional(date("yyyy-MM-dd")), 
    "categories" -> nonEmptyText, 
    "tags" -> nonEmptyText, 
    "author" -> nonEmptyText 
)(Post.apply)(Post.unapply) 
) 

我也改变了我的Post.scala文件:

object PostDAO extends SalatDAO[Post, ObjectId](
collection = MongoConnection()("blog")("posts")) 

object Post { 

    def create(title: String, content: String, posted: Date = new Date(), modified: Option[Date] = None, categories: String, tags: String, author: String) { 
    PostDAO.insert(Post(
     title = title, 
     content = content, 
     posted = posted, 
     modified = modified, 
     categories = categories, 
     tags = tags, 
     author = author 
     )) 
    } 

    def delete(id: String) { 
    PostDAO.remove(MongoDBObject("_id" -> new ObjectId(id))) 
} 
}