Grails中的验证错误
我对圣杯一无所知。我从过去几天开始学习。我遵循这一点来创建一个博客 我有3个域帖子,评论和评论员。如果评论属于一篇文章,那么一篇文章有许多评论,一篇评论有一位评论员,而一位评论员属于一篇评论。看看我的代码 域类--- 后域类-Grails中的验证错误,grails,groovy,grails-2.0,grails-domain-class,Grails,Groovy,Grails 2.0,Grails Domain Class,我对圣杯一无所知。我从过去几天开始学习。我遵循这一点来创建一个博客 我有3个域帖子,评论和评论员。如果评论属于一篇文章,那么一篇文章有许多评论,一篇评论有一位评论员,而一位评论员属于一篇评论。看看我的代码 域类--- 后域类- class Post { static hasMany = [comments:Comment] String title String teaser String content Date lastUpdated Boolean published = false
class Post {
static hasMany = [comments:Comment]
String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments
static constraints = {
title(nullable:false, blank:false, length:1..50)
teaser(length:0..100)
content(nullable:false, blank:false)
lastUpdated(nullable:true)
published(nullable:false)
}
}
class Comment implements Comparable {
static belongsTo = Post
Post post
String comment
Commentator who = new Commentator()
Date dateCreated
public int compareTo(Object o) {
return dateCreated.compareTo(o.dateCreated)
}
static constraints = {
}
}
class Commentator {
static belongsTo = Comment
String name
String url
String email
Comment comment
static constraints = {
name(nullable:false, blank:false)
url(nullable:true, blank:true, url:true)
email(nullable:true, blank:true, email:true)
}
}
注释域类-
class Post {
static hasMany = [comments:Comment]
String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments
static constraints = {
title(nullable:false, blank:false, length:1..50)
teaser(length:0..100)
content(nullable:false, blank:false)
lastUpdated(nullable:true)
published(nullable:false)
}
}
class Comment implements Comparable {
static belongsTo = Post
Post post
String comment
Commentator who = new Commentator()
Date dateCreated
public int compareTo(Object o) {
return dateCreated.compareTo(o.dateCreated)
}
static constraints = {
}
}
class Commentator {
static belongsTo = Comment
String name
String url
String email
Comment comment
static constraints = {
name(nullable:false, blank:false)
url(nullable:true, blank:true, url:true)
email(nullable:true, blank:true, email:true)
}
}
评论员域类-
class Post {
static hasMany = [comments:Comment]
String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments
static constraints = {
title(nullable:false, blank:false, length:1..50)
teaser(length:0..100)
content(nullable:false, blank:false)
lastUpdated(nullable:true)
published(nullable:false)
}
}
class Comment implements Comparable {
static belongsTo = Post
Post post
String comment
Commentator who = new Commentator()
Date dateCreated
public int compareTo(Object o) {
return dateCreated.compareTo(o.dateCreated)
}
static constraints = {
}
}
class Commentator {
static belongsTo = Comment
String name
String url
String email
Comment comment
static constraints = {
name(nullable:false, blank:false)
url(nullable:true, blank:true, url:true)
email(nullable:true, blank:true, email:true)
}
}
控制器类--
后置控制器--
和控制器类--
视图部分--
张贴/查看---
由于postId未通过
,因此出现错误。请帮助..我认为您在控制器中使用的是params.postId,而在id参数中发送post.id的视图中,您应该在params.id中获取post id,而不是params.postId,我认为如果您将控制器中的编辑功能更改为:
def edit = {
if(params.id){
render(view:'edit',
model:[
comment:new Comment(),
postId:params.id])
}else{
render "No post Id available"
}
}
此外,在您的post控制器中,您还可以拨打电话:
render(view:'edit', model:[post:post])
现在您正在发送post对象以查看:编辑,但在使用postId的“编辑gsp”页面中,我认为您应该选择其中任何一个,或者发送对象并在edit.gsp中使用object.id,或者如果使用postId,则应将渲染更改为:
render(view:'edit', model:[postId:post.id])
并将保存操作更改为:
def save = {
def comment = new Comment(params)
comment.dateCreated = new Date();
Post postInstance = Post.get(params.postId)
if(postInstance){
comment.post = postInstance
if(comment.save(failOnError:true)) {
redirect(
controller:'post',
action:'view',
id:params.postId)
} else {
render(view:'edit',
model:[comment:comment,
postId:params.postId])
}
}else{
render "No Post instance found"
}
}
如果您还有任何问题,请告诉我:)能否将您的隐藏属性设置为文本,只是为了检查GSP页面上是否有postId变量我将隐藏属性更改为文本。它不显示任何id。意味着在将postId:params.postId更改为
postId:params.id
后,不会获取id。它的显示id但仍然获得相同的验证错误。请检查上面编辑的答案,并更改保存方法以附加post实例,您在编辑表单的postId参数中有post id,因此,从该参数获取一个post实例,并添加一个检查,如果您找到了post实例,然后将其保存。您可以给我您的Skype ID吗??