더북(TheBook)

10.4.4 포스트의 Show 페이지에서 댓글 보여주기

포스트의 Show 뷰에 댓글을 보여주는 코드를 추가해 보자.

▼ app/views/Post/Show.html

{{set . "title" "Show Post"}}
{{template "header.html" .}}
  
<p>
    <b>Title: </b>
    {{ .post.Title}}
</p>
 
<p>
    <b>Body: </b>
    {{ .post.Body}}
</p>
  
<h4>Comments: </h4>
{{range .post.Comments}}
    <p>
        <mark>
            <b>{{.Commenter}}:</b>
        </mark>
        {{.Body}}
        <small>
            ({{formatDate .CreatedAt}})
        </small>
    </p>
{{end}}
  
<h4>Add a comment:</h4>
<form method="POST" action="{{url "Comment.Create" .post.Id}}">
    <div>
        {{with $field := field "commenter" .}}
            <label for="commenter">Name</label>
            <input type="text" id="commenter" name="{{$field.Name}}" placeholder="Commenter"/>
        {{end}}
    </div>
    <div>
        {{with $field := field "body" .}}
            <label for="body">Body</label>
            <input type="text" id="body" name="{{$field.Name}}" placeholder="Body"/>
        {{end}}
    </div>
    <button type="submit">Add comment</button>
</form>
  
<a href="{{url "Post.Edit" .post.Id}}">Edit</a>
<a href="{{url "Post.Index"}}">Back</a>
  
{{template "footer.html" .}}

이제 블로그에 포스트와 댓글을 쓸 수 있게 되었다.

그림 10-14 댓글 작성
그림 10-15 댓글 작성 완료
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.