각 화면에서 isAdmin으로 버튼 제한
app/views/Comment/_comment.html 파일에서 삭제 버튼은 관리자에게만 보이게 수정한다.
▼ app/views/Comment/_comment.html
{{$post := .post}} {{$currentUser := .currentUser}} {{range .post.Comments}} <p> <b><mark>{{.Commenter}}</mark> :</b> {{.Body}} <small class="text-muted"> ({{formatDate .CreatedAt}}) </small> {{if isAdmin $currentUser}} <a href="{{url "Comment.Destroy" $post.Id .Id}}" class="btn btn-link btn-sm">Destroy</a> {{end}} </p> {{end}}
템플릿의 {{range ...}} 루프 내에서는 range의 매개변수로 전달하는 컬렉션의 내부 요소로만 접근할 수 있다. 이 코드에서는 {{range .post.Comments}} 루프 내에서 currentUser에 접근해야 한다. 그래서 {{range ...}} 루프를 시작하기 전에 {{$currentUser := .currentUser}}로 새로운 $currentUser 변수를 생성하고, {{range ...}} 루프 내에서는 $currentUser를 사용한다.