2013-07-05 6 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)   
} 
} 

컨트롤러 Cla SSES -

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를 사용하고 있는데, id 매개 변수에서 post.id를 보내는보기에서 params.postId 대신 params.id에 게시 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 페이지를 편집하십시오, 나는 그들 중 하나를 선택해야한다고 생각합니다. 객체를 찾고 편집시 object.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" 
} 
} 

: 당신이 postId를 사용하는 경우 GSP는, 또는 당신은으로 렌더링하여 변경해야합니다 :

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

또한 당신의 저장 작업을 변경

+0

GSP 페이지에 postId 변수가 있는지 확인하기 위해 텍스트에 숨겨진 속성을 만들 수 있습니까? –

+0

확인하고 있습니다. –

+0

숨겨진 속성을 텍스트로 변경합니다. 그 어떤 이드도 보이지 않습니다. 수단 ID가 반입되지 않습니다. –