더북(TheBook)

New 뷰 작성

New 뷰는 비어있는 post 객체를 사용자에게 보여주고, 포스트를 작성할 수 있는 폼을 제공한다. app/views/Post/New.html 파일을 생성하고 다음과 같이 코드를 작성하자.

▼ app/views/Post/New.html

{{set . "title" "New Post"}}
{{template "header.html" .}}
  
<form method="POST" action="{{url "Post.Create"}}">
    {{$post := .post}}
    {{with $field := field "title" .}}
        <div>
            <label for="title">Title</label>
            <input type="text" id="title" name="{{$field.Name}}" placeholder="Enter title" value="{{if $field.Flash}}{{$field.Flash}}{{else}}{{$post.Title}}{{end}}"/>
        </div>
    {{end}}
  
    {{with $field := field "body" .}}
        <div>
            <label for="title">Body</label>
            <textarea id="body" name="{{$field.Name}}">
                {{if $field.Flash}}{{$field.Flash}}{{else}}{{$post.Body}}{{end}}
            </textarea>
        </div>
    {{end}}
    <button type="submit">Create Post</button>
</form>
  
<a href="{{url "Post.Index"}}">Back</a>
  
{{template "footer.html" .}}

사용자가 포스트를 작성한 후 Create Post 버튼을 클릭하면 브라우저는 포스트 컨트롤러의 Create 액션으로 생성된 포스트 정보를 전달한다. $field를 사용하는 방법은 Revel 공식 문서를 참고하기 바란다.

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.