- Action Pack
- Action Controller
- Action Dispatch
- Action View
- Action Mailer
- Active Model
- Active Record
- Active Resource
- Active Support
- Railties
액션 팩은 액션 컨트롤러와 액션 뷰를 포함하는 젬으로 "MVC"에서 View와 Controller를 포함하고 있다.
액션 컨트롤러는 레일즈 애플리케이션에서 컨트롤러를 담당하는 컴포넌트이다. 액션 컨트롤러 프레임워크는 레일즈 애플리케이션으로 들어오는 요청을 처리하고, 파라미터를 추출하고, 의도된 액션으로 보낸다. 액션 컨트롤러는 세션(session)관리, 템플릿(template) 렌더링, 리다이렉트(redirect) 관리 서비스를 포함한다.
- 컨트롤러와 액션의 생성
- 명령라인 설명 rails generate : 새로운 코드를 생성하는 명령 controller welcome : welcome 컨트롤러를 생성한다. app/controllers/welcome_controller.rb index : 액션을 생성한다. 자동으로 app/views/welcome/index.html.erb 뷰가 생성된다. (여기서 erb는 embedded ruby를 의미한다. 이는 erb 시스템으로 부터 확장했다는 의미로 controller에 정의한 변수를 사용할 수 있으며 <%= [@NAME] %>의 형식으로 접근할 수 있다)
- route get 'welcome/index' config/routes.rb 파일에 get 'welcome/index' 를 추가한다. 이로인해 http://서버ip:포트번호/welcome/index 로 접근하면 app/views/welcome/index.html.erb에서 출력하는 내용을 볼 수있다. 서버 내부적으로는 레일즈 엔진의 라우터가 접근 경로를 보고 welcome 컨트롤러의 index 액션을 호출한다. 코드 상으로는 파일(app/controllers/welcome_controller.rb) 내의 index 함수가 실행된다. 이후 레일즈 프레임워크 규칙에 따라 app/views 디렉토리에 있는 해당 컨트롤러의 이름과 동일한 디렉토리에 있는 액션명의 erb 파일(index.html.erb )을 뷰 템플릿(app/views/welcome/index.html.erb)으로 이용하여 응답으로 보낼 파일(.html)을 렌더링한다.
- root 라우트 설정 config/routes.rb 파일의 get'welcome/index' 를 root 'welcome#index' 로 변경하여 "http://서버ip:포트번호"로의 접근을 welcome 컨트롤러의 index 액션이 호출되도록 변경할 수 있다. 여기서 컨트롤러와 액션명 사이에 '/' 가 아니라 '#'이 와야하며 root의 정의는 routes.rb 파일의 최상위에 위치해야 한다.
$ bin/rails generate controller welcome index
create app/controllers/welcome_controller.rb
route get 'welcome/index'
invoke erb
create app/views/welcome
create app/views/welcome/index.html.erb
invoke test_unit
create test/controllers/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
create test/helpers/welcome_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/welcome.js.coffee
invoke scss
create app/assets/stylesheets/welcome.css.scss
초보자를 위한 레일스 가이드북(a.k.a, 초레가) 를 따라하며 정리한 글입니다.
댓글 없음:
댓글 쓰기