[Ruby on Rails] scaffold 공부하기
#Scaffold 란?
Rails는 Ruby 언어로 만든 웹 프레임워크입니다. 일반적인 웹 프레임워크와 비슷하게 MVC(Model-View-Controller)구조를 갖고 있는데요. 보통 rails g controller ctr_name, rails g model model_name의 명령어로 각각의 모델과 컨트롤러를 생성해주고, routes.rb에서 url 패턴을 잡아
줘야합니다.
하지만 rails에서 지원하는 scaffold기능은 위와 같은 여러개의 번거로운 작업을 한 번에 해결해주는데요. rails g scaffold name_sapce [options] 의 명령어 한번이면 model과 controller 그리고 controller 안에 CRUD의 7가지 기능(index, show, new, create, edit, update, destroy)을 자동으로 생성한 후, routes.rb 설정까지 모두 끝내줍니다.
#언제 scaffold를 사용할까?
scaffold는 위에서 설명한바와 같이 상당히 간편합니다. 하지만 간편하다고 해서 어디에서나 사용되는 것은 아닌데요. 주로 프로토타입이다. 설명, 홍보를 위한 데모버전을 빠르게 개발하기 위해 사용하곤 합니다. 저는 얼마전에 신촌에서 진행한 해커톤에서 개발의 속도를 위해 처음 scaffold를 제대로 사용해 보았습니다.
하나하나 controller 설정과 routes 설정을 해주지 않아도 돼서 편리한 점은 있었지만, 이미지나 타 gem들을 사용할 때 많은 불편과 충돌이 발생했었 던 것이 기억이 납니다. 저는 주로 front를 담당해서 문제를 정확히 파악하진 못했는데, 멋사의 이두희 대장님 덕분에 빠르게 해결할 수 있었습니다.. ㅋㅋ
#scaffold 만들기
$ rails g scaffold Note title:string content:text
터미널에 위와같은 명령어로 Note라는 scaffold를 생성합니다.
- controller
app/controllers/notes_controller.rb
- views
app/views/notes/_form.html.erb
app/views/notes/eidt.html.erb
app/views/notes/index.html.erb
app/views/notes/show.html.erb
app/views/notes/new.html.erb
- model
app/models/note.rb
- migrate file
db/migrate/20180704043848_create_notes.rb
외와 같은 파일들이 생성되는데요. scaffold를 생성하면 rails가 알아서 MVC패턴에 맞게 구성요소들을 생성해주는 모습입니다. 참 대견한 녀석이네요 ^^
#controller 열어보기
class NotesController < ApplicationController
before_action :set_note, only: [:show, :edit, :update, :destroy]
# GET /notes
# GET /notes.json
def index
@notes = Note.all
end
# GET /notes/1
# GET /notes/1.json
def show
end
# GET /notes/new
def new
@note = Note.new
end
# GET /notes/1/edit
def edit
end
# POST /notes
# POST /notes.json
def create
@note = Note.new(note_params)
respond_to do |format|
if @note.save
format.html { redirect_to @note, notice: 'Note was successfully created.' }
format.json { render :show, status: :created, location: @note }
else
format.html { render :new }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /notes/1
# PATCH/PUT /notes/1.json
def update
respond_to do |format|
if @note.update(note_params)
format.html { redirect_to @note, notice: 'Note was successfully updated.' }
format.json { render :show, status: :ok, location: @note }
else
format.html { render :edit }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end
# DELETE /notes/1
# DELETE /notes/1.json
def destroy
@note.destroy
respond_to do |format|
format.html { redirect_to notes_url, notice: 'Note was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_note
@note = Note.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def note_params
params.require(:note).permit(:title, :content)
end
end
위와 같이 crud의 7가지 controller들이 생성되어 있습니다. 루비가 가장 싫어하는 중복 코드또한 set_note와 note_params라는 private한 함수를 생성하여 중복을 없애주었네요.
#scaffold의 form_for 이해하기
form_for은 rails에서 제공하는 기능 중에 하나인데요. scaffold로 생성된 _from.html.erb 파일을 열어보면 우리가 흔히 알고 있는 <form></form>태그, <input >태그를 볼수 없고, <% form_for %>이라는 이상한 녀석이 보입니다.
- _form.html.erb 코드
<%= form_for(note) do |f| %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
form_for은 위와 같은 모습입니다. 서버를 실행하여 http://localhost:3000/notes/new로 접속해보면 아래와 같은 모습인데요. 개발자도구를 실행하여 form_for의 감추어진 모습을 분석해 보았습니다.
- 실제 form_for의 모습
개발자도구(f12)를 통해 new.html.erb의 모습을 훔쳐보면 form 태그의 보안을 위한 authenticity 토큰과 utf-8과 같은 인코딩 설정 등 중요기능들을 form_for가 가지고 있다는 것을 알 수 있습니다. 아래와 같은 설정들을 input태그를 통해 일일이 해줘야 하는 번거로움이 있지만, form_for와 <%= %>의 레일즈 템플릿 태그를 사용하면 간편한 코드를 통해 아래의 설정들을 자동으로 할 수 있다는 것을 알게되었습니다.