TIL / 20211017 / RubyOnRails scaffold 이해하기
scaffold(스캐폴딩)란?
CRUD와 연관된 기본 기능들을 갖춘 프로젝트의 뼈대를 만들어준다.
언제 사용해?
1.Rails 코드의 정석을 학습할 때
2.빠르게 동작하는 프로젝트를 만들 때
3.로직이 일반적인 CRUD를 벗어나지 않을 때
scaffold는 모델과 달리 CRUD 기능을하는 view와 controller,routes도 자동 생성됨
1.RESTful과 resources
rails g scaffold Post title:string content:text
2.controller
명령어 실행시 여러 파일이 설정됨
종류 : active_record, 경로, 컴트롤러, 뷰파일, helper, jbuilder, assets 등 생성됨
filter 메서드: 직전(before), 직후(after), 모두(arround)에 실행하는 메서드
action method는 액션 이름과 동일하게 자동 생성된 view와 연결됨
view와 연결x 하려면 앞에 private, protected를 달면 됨
before_action :set_post, only: [:show, :edit, :update, :destroy]
after_action :set_post
before_action :set_post, except: [:show, :edit, :update, :destroy]
위 only 설정이 없으면 모든 action에서 filter 적용
except는 지정한 action 이외 filter 적용
respond_to
언제? 서버에서 응답 종류를 분기할 때 사용
format.type 형식으로 원하는 형식 지원함 ex.format.html은 html 형식 지원
예시
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
아래는 이어서 나중에 작성..
3.View
- render
- link_to
- form_with
Author And Source
이 문제에 관하여(TIL / 20211017 / RubyOnRails scaffold 이해하기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jeongyunjang/TIL-20211017-RubyOnRails-scaffold-이해하기저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)