2013-07-05 90 views
0

我在Grails中是全新的。我從過去幾天開始學習。我正在關注這個tutorial來創建博客。Grails驗證錯誤

我有3個域的帖子,評論和評論員。如果評論屬於帖子,帖子有很多評論,評論有一個評論員和一個評論員屬於評論。看看我的代碼..

域類---

後域類 -

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)   
} 
} 

Controller Cla的SSE -

PostController中 -

class PostController { 

    def defaultAction = 'list' 

    def edit = { 
     def post = Post.get(params.id) 
     if(!post) { 
     post = new Post() 
    } 
    render(view:'edit', model:[post:post]) 
    } 

    def list = { 
     render(
     view:'list', 
     model:[posts:Post.list(
       sort:'lastUpdated', 
       order:'desc')]) 
     } 

     def save = { 
    def post = loadPost(params.id) 
    post.properties = params 
    if(post.save()) { 
    redirect(action:'list') 
    } else { 
    render(view:'edit', model:[post:post]) 
    } 
    } 

    private loadPost(id) { 
def post = new Post(); 
if(id) { 
    post = Post.get(id) 
} 
return post 
    } 

    def view = { 
    render(view:'view', model:[post:Post.get(params.id)]) 
    } 

    } 

和CommentController類 -

class CommentController { 

def edit = { 
    render(view:'edit', 
      model:[ 
        comment:new Comment(), 
        postId:params.postId]) 
} 

def save = { 
    def comment = new Comment(params) 
    comment.dateCreated = new Date(); 
    comment.post = Post.get(params.id) 
    if(comment.save()) { 
     redirect(
       controller:'post', 
       action:'view', 
       id:params.postId) 
    } else { 
     render(view:'edit', 
       model:[comment:comment, 
         postId:params.postId]) 
    } 
} 
} 

查看部分 -

發表/查看---

<%@ page contentType="text/html;charset=UTF-8" %> 
<html> 
<head> 
    <title>${post.title}</title> 
    <r:require modules="bootstrap"/> 
<meta name="layout" content="main"/> 
</head> 
<body> 
<div class="well"> 
<h1>${post.title}</h1> 
<p>${post.teaser}</p> 
<div>${post.content}</div> 
<g:link controller="comment" action="edit" id="${post.id}" class="btn btn-success"> 
    Add comment 
</g:link> 
<g:each in="${post.comments}" var="comment"> 
    <div class="comment"> 
    <p>${comment.comment}</p> 
    <p>Made by: ${comment.who.name} on ${comment.dateCreated}</p> 
    </div> 
</g:each> 
</div> 
</body> 
</html> 

當我點擊添加評論那麼其所謂的意見/ edit.gsp這是在這裏 -

edit.gsp -

<%@ page contentType="text/html;charset=UTF-8" %> 
<html> 
<head> 
    <title>Create Comment</title> 
    </head> 
    <body> 
    <h1>Create a comment</h1> 
    <div id="validationerrors"> 
    <g:renderErrors bean="${comment}"/> 
     </div> 
    <g:form controller="comment" action="save"> 
    <g:hiddenField name="postId" value="${postId}"/> 
    <dl> 
     <dt>Your name:</dt> 
     <dd> 
     <g:textField name="who.name" value="${comment.who.name}"/> 
     </dd> 
     <dt>Your email:</dt> 
     <dd> 
     <g:textField name="who.email" value="${comment.who.email}"/> 
     </dd> 
     <dt>Your website/blog:</dt> 
     <dd> 
     <g:textField name="who.url" value="${comment.who.url}"/> 
     </dd> 
     <dt>Add your comment:</dt> 
     <dd> 
     <g:textArea name="comment" value="${comment.comment}" rows="20" cols="50"/> 
     </dd> 
    </dl> 
    <g:submitButton name="submit" value="Save"/> 
    </g:form> 
    </body> 
    </html> 

當我點擊保存按鈕則顯示驗證錯誤

Property [post] of class [class groovypublish.Comment] cannot be null 

的錯誤是因爲postId沒有收到<g:hiddenField name="postId" value="${postId}"/>。請幫助..

回答

1

我想在你的控制器中使用params.postId,而從視圖發送post.id在ID參數,所以你應該在params.id而不是params.postId,我得到職位ID如果你改變你的編輯功能的控制器認爲:

def edit = { 
    if(params.id){ 
     render(view:'edit', 
      model:[ 
        comment:new Comment(), 
        postId:params.id]) 
    }else{ 
     render "No post Id available" 
    } 
    } 

,並在您的文章控制器,你有電話:

render(view:'edit', model:[post:post]) 

現在您發送日誌對象視圖:編輯,但在編輯你正在使用postId的gsp頁面,我想你應該選擇其中的任何一個,要麼nd對象並在編輯中使用object.id。GSP,或者如果您正在使用,然後帖子ID,你應該改變你的呈現爲:

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" 
} 
} 

讓我知道如果你還有任何問題:)

+0

你能否將你的隱藏屬性改爲文本,只是爲了檢查它是否在GSP頁面上有postId變量 –

+0

好吧我正在檢查 –

+0

我將隱藏的文件改爲文本。它沒有顯示任何ID。意味着Id不會被提取 –