Scaffolding

scaffolding์ด๋ž€ '๊ธฐ๋ฐ˜'์ด๋ผ๋Š” ์˜๋ฏธ๋กœ, ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์„ ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ณจ๊ฒฉ(๊ธฐ๋ฐ˜)์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋Šฅ

1. rails project ์ƒ์„ฑ

rails new learn_project

2. scaffold ์ƒ์„ฑ

rails g scaffold name field:type [โˆ™โˆ™โˆ™] [options] 
#name : ๋ชจ๋ธ์ด๋ฆ„
#field : ํ•„๋“œ ์ด๋ฆ„
#type : ์ž๋ฃŒํ˜•
#options :๋™์ž‘์˜ต์…˜
invoke  active_record
create    db/migrate/20170123090643_create_posts.rb
create    app/models/post.rb
invoke    test_unit
create      test/models/post_test.rb
create      test/fixtures/posts.yml
invoke  resource_route
route    resources :posts
invoke  scaffold_controller
create    app/controllers/posts_controller.rb
invoke    erb
create      app/views/posts
create      app/views/posts/index.html.erb
create      app/views/posts/edit.html.erb
create      app/views/posts/show.html.erb
create      app/views/posts/new.html.erb
create      app/views/posts/_form.html.erb
invoke    test_unit
create      test/controllers/posts_controller_test.rb
invoke    helper
create      app/helpers/posts_helper.rb
invoke      test_unit
invoke    jbuilder
create      app/views/posts/index.json.jbuilder
create      app/views/posts/show.json.jbuilder
create      app/views/posts/_post.json.jbuilder
invoke  assets
invoke    coffee
create      app/assets/javascripts/posts.coffee
invoke    scss
create      app/assets/stylesheets/posts.scss
invoke  scss
create    app/assets/stylesheets/scaffolds.scss

scaffold๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด์„œ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ ํŒŒ์ผ๋“ค ์ด๋‹ค. ์ด ์ค‘์— posts_controller.rb , route, ๊ฐ์ข… viewํŒŒ์ผ๋“ค์„ ์‚ดํŽด ๋ณผ ๊ฒƒ์ด๋‹ค.

RESTful

1. HTTP๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

  • HTTP๋ž€ HyperText Transport Protocol์˜ ์•ฝ์ž๋กœ ์›น์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ๊ฐ„์˜ ๋ฌธ์„œ๋ฅผ ๊ตํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํ”„๋กœํ† ์ฝœ์ด๋‹ค.

  • World Wide Web( WWW )์˜ ๋ถ„์‚ฐ๋˜์–ด ์žˆ๋Š” Server์™€ Client ๊ฐ„์— Hypertext๋ฅผ ์ด์šฉํ•œ ์ •๋ณด๊ตํ™˜์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜๋Š” ํ†ต์‹  ๊ทœ์•ฝ์ด๋‹ค.

    ํ•˜์ดํผํ…์ŠคํŠธ๋Š” ๋ฌธ์„œ ์ค‘๊ฐ„์ค‘๊ฐ„์— ํŠน์ • ํ‚ค์›Œ๋“œ๋ฅผ ๋‘๊ณ  ๋ฌธ์ž๋‚˜ ๊ทธ๋ฆผ์„ ์ƒํ˜ธ ์œ ๊ธฐ์ ์œผ๋กœ ๊ฒฐํ•ฉํ•˜์—ฌ ์—ฐ๊ฒฐ์‹œํ‚ด์œผ๋กœ์จ, ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์„œ๋ผ ํ• ์ง€๋ผ๋„ ํ•˜๋‚˜์˜ ๋ฌธ์„œ์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋ฉด์„œ ์ฐธ์กฐํ•˜๊ธฐ ์‰ฝ๋„๋ก ํ•˜๋Š” ๋ฐฉ์‹์„ ์˜๋ฏธํ•œ๋‹ค.

    ์ธํ„ฐ๋„ท ์ฃผ์†Œ๋ฅผ ์ง€์ •ํ•  ๋•Œ 'http://www....' ์™€ ๊ฐ™์ด ํ•˜๋Š” ๊ฒƒ์€ www๋กœ ์‹œ์ž‘๋˜๋Š” ์ธํ„ฐ๋„ท ์ฃผ์†Œ์—์„œ ํ•˜์ดํผํ…์ŠคํŠธ ๋ฌธ์„œ์˜ ๊ตํ™˜์„ http ํ†ต์‹ ๊ทœ์•ฝ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ผ๋Š” ๋œป์ด๋‹ค.

  • http์˜ ์ฒซ๋ฒˆ์งธ ๋ฒ„์ „์€ ์ธํ„ฐ๋„ท์„ ํ†ตํ•˜์—ฌ ๊ฐ€๊ณต๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•œ ๋‹จ์ˆœํ•œ ํ”„๋กœํ† ์ฝœ์ด์—ˆ์œผ๋‚˜, ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ „์†ก๊ณผ ์š”๊ตฌยท์‘๋‹ต์— ๋Œ€ํ•œ ์ˆ˜์ • ๋“ฑ ๊ฐ€๊ณต๋œ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋Š” ํ”„๋กœํ† ์ฝœ๋กœ ๊ฐœ์„ ๋˜์—ˆ๋‹ค.

2. HTTP method์—๋Š” ์–ด๋–ค๊ฒƒ์ด ์žˆ๊ณ  ์™œ ์žˆ๋Š”๊ฐ€?

GET

  1. URL์— ํ•ด๋‹นํ•˜๋Š” ์ž๋ฃŒ์˜ ์ „์†ก์„ ์š”์ฒญํ•œ๋‹ค.

  2. ๋ฐ์ดํ„ฐ๊ฐ€ URL์— ๋…ธ์ถœ๋œ๋‹ค.

  3. ์ธ์ฝ”๋”ฉ/๋””์ฝ”๋”ฉ์˜ ๊ณผ์ •์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— POST๋ณด๋‹ค ๋น ๋ฅด๋‹ค.

  4. URL์˜ ๊ธธ์ด ์ œ์•ฝ์œผ๋กœ ์ธํ•ด ๋งŽ์€ ๋ฐ์ดํ„ฐ ์ „์†ก์€ ๋ฌด๋ฆฌ์ด๋‹ค.

POST

  1. ์„œ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๋ฅผ ๋ณด๋‚ธ๋‹ค.

  2. ๋ฐ์ดํ„ฐ๋Š” HTTP Boby์— ์ˆจ๊ฒจ์„œ ์„œ๋ฒ„๋กœ ์ „์†กํ•œ๋‹ค.

  3. GET์œผ๋กœ ๋ณด๋‚ผ์ˆ˜ ์—†๋Š” ์ž๋ฃŒ๋ฅผ ์ „์†กํ• ๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

HEAD

  1. GET๊ณผ ๊ฐ™์€ ์š”์ฒญ์ด์ง€๋งŒ, ์›น ์„œ๋ฒ„์—์„œ ํ—ค๋” ์ •๋ณด ์ด์™ธ์—๋Š” ์–ด๋–ค ๋ฐ์ดํ„ฐ๋„ ๋ณด๋‚ด์ง€ ์•Š๋Š”๋‹ค.

  2. ์›น ์„œ๋ฒ„์˜ ๋‹ค์šด ์—ฌ๋ถ€ ์ ๊ฒ€(Health Check)์ด๋‚˜ ์›น ์„œ๋ฒ„ ์ •๋ณด(๋ฒ„์ „ ๋“ฑ)๋“ฑ์„ ์–ป๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

OPTIONS

  1. ์„œ๋ฒ„๊ฐ€ ํŠน์ • URL์— ๋Œ€ํ•ด ์–ด๋– ํ•œ HTTP Method๋ฅผ ์ง€์›ํ•˜๋Š”์ง€ ๋ฌป๋Š”๋‹ค.

PUT

  1. ํ•ด๋‹น URL์— ์ž๋ฃŒ๋ฅผ ์ €์žฅํ•œ๋‹ค.(POST์™€ ์œ ์‚ฌํ•œ ์ „์†ก ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ํ—ค๋” ์ด์™ธ์— ๋ฉ”์‹œ์ง€(๋ฐ์ดํ„ฐ)๊ฐ€ ํ•จ๊ป˜ ์ „์†ก๋œ๋‹ค.)

  2. ์›๊ฒฉ์ง€ ์„œ๋ฒ„์— ์ง€์ •ํ•œ ์ฝ˜ํ…์ธ ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋ฉฐ ํ™ˆํŽ˜์ด์ง€ ๋ณ€์กฐ์— ๋งŽ์ด ์•…์šฉ๋˜๊ณ  ์žˆ๋‹ค.

DELETE

  1. ํ•ด๋‹น URL์˜ ์ž๋ฃŒ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

TRACE

  1. ์ด์ „์— ์š”์ฒญํ•œ ๋‚ด์šฉ์„ ๋“ค์„ ๊ฒƒ์„ ์š”์ฒญํ•œ๋‹ค.

  2. ์›๊ฒฉ์ง€ ์„œ๋ฒ„์— Loopback(๋ฃจํ”„๋ฐฑ) ๋ฉ”์‹œ์ง€๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

CONNECT

  1. ํ”„๋ก์‹œ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์š”์ฒญ.

PATCH

  1. ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ ์ธ ์ˆ˜์ •์„ ์ ์šฉํ•œ๋‹ค.

3. RESTful ์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€.

REpresentational State Transfer์˜ ์•ฝ์ž๋กœ ์žฅ๋น„๊ฐ„ ํ†ต์‹ ์„ ์œ„ํ•ด CORBA, RPC, SOAP๋“ฑ์˜ ๋ณต์žกํ•œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ , ๊ฐ„๋‹จํ•˜๊ฒŒ HTTP๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋‹ค.

HTTP URI๋ฅผ ํ†ตํ•ด Resource๋ฅผ ๋ช…์‹œํ•˜๊ณ , HTTP Method(Post, Get, Put, Delete)๋ฅผ ํ†ตํ•ด ํ•ด๋‹น Resource์— ๋Œ€ํ•œ CRUD Operation์„ ์ ์šฉํ•œ๋‹ค. ์ฆ‰, REST๋Š” ROA(Resource Oriented Architecture) ์„ค๊ณ„์˜ ์ค‘์‹ฌ์— Resource๊ฐ€ ์žˆ๊ณ  HTTP Method๋ฅผ ํ†ตํ•ด Resource๋ฅผ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์„ค๊ณ„๋œ ์•„ํ‚คํ…์ณ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

REST์˜ ์žฅ์ 

  • OPENAPI๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์‰ฝ๋‹ค.

  • ๋ฉ€ํ‹ฐํ”Œ๋žซํผ ์ง€์› ๋ฐ ์—ฐ๋™์ด ์šฉ์ดํ•˜๋‹ค.

  • ์›ํ•˜๋Š” ํƒ€์ž…์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„์ˆ˜ ์žˆ๋”ฐ. (XML, JSON, RSS )

  • ๊ธฐ์กด ์›น ์ธํ”„๋ผ๋ฅผ(HTTP)๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค ( ๋ฐฉํ™”๋ฒฝ, ์žฅ๋น„ ์š”๊ฑด ๋ถˆํ•„์š” )

  • ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๋‹ค

  • ์„ธ์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ฐ๊ฐ์˜ ์š”์ฒญ์— ๋…๋ฆฝ์ .

REST์˜ ๋‹จ์ 

  • ํ‘œ์ค€์ด ์—†์–ด์„œ ๊ด€๋ฆฌ๊ฐ€ ์–ด๋ ต๋‹ค.

  • ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ 4๊ฐ€์ง€ ๋ฐ–์— ์—†๋‹ค.

  • ๋ถ„์‚ฐํ™˜๊ฒฝ์—๋Š” ๋ถ€์ ํ•ฉํ•˜๋‹ค.

  • HTTPํ†ต์‹  ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ๋งŒ ์ง€์›ํ•œ๋‹ค.

REST์˜ ํŠน์ง•

  • ํด๋ผ์ด์–ธํŠธ/์„œ๋ฒ„ ๊ตฌ์กฐ : ์ผ๊ด€์ ์œผ๋กœ ๋…๋ฆฝ๋˜์–ด์•ผ ํ•œ๋‹ค.

  • ๋ฌด์ƒํƒœ(Stateless) : ๊ฐ์š”์ฒญ ๊ฐ„ ํด๋ผ์ด์–ธํŠธ์˜ Context๋Š” ์„œ๋ฒ„์— ์ €์žฅ๋˜์–ด์„œ๋Š” ์•ˆ ๋œ๋‹ค.

  • ์บ์‹œ๊ฐ€๋Šฅ(Cacheable) : WWW์—์„œ์™€ ๊ฐ™์ด ํด๋ผ์ด์–ธํŠธ๋Š” ์‘๋‹ต์„ Caching ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

  • ๊ณ„์ธตํ™”(Layered System) : ํด๋ผ์ด์–ธํŠธ๋Š” ๋ณดํ†ต ๋Œ€์ƒ ์„œ๋ฒ„์— ์ง์ ‘ ์—ฐ๊ฒฐ ๋˜๋Š” ์ค‘๊ฐ„ ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋˜๋Š”์ง€ ๋ชจ๋ฅธ๋‹ค.

  • Code on demand(option) : ์ž๋ฐ” ์• ํ”Œ๋ฆฟ/ ์ž๋ฐ”์Šคํฌ๋ฆฝ์˜ ์ œ๊ณต์œผ๋กœ ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์‹คํ–‰ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๋กœ์ง์„ ์ „์†กํ•˜์—ฌ, ๊ธฐ๋Šฅ์„ ํ™•์žฅ ํ• ์ˆ˜ ์žˆ๋‹ค.

  • ์ธํ„ฐํŽ˜์ด์Šค ์ผ๊ด€์„ฑ : ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๊ณ , ์ž‘์€ ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ, ํด๋ผ์ด์–ธํŠธ-์„œ๋ฒ„ ํŒŒํŠธ ๋ณ„๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ๊ฐœ์„  ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

  • ์ž์ฒด ํ‘œํ˜„๊ตฌ์กฐ(Self-Descriptiveness) : API ๋ฉ”์‹œ์ง€๋งŒ ๋ณด๊ณ ๋„ ์–ด๋–ค API์ธ์ง€๋ฅผ ์ดํ•ด ํ• ์ˆ˜ ์žˆ๋Š” ์ž์ฒด ํ‘œํ˜„ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„๋‹ค.

4. rake routes๊ฐ€ ๋งํ•ด์ฃผ๋Š” ๊ฒƒ์€?

routes.rb

Rails.application.routes.draw do
  resources :posts
end

resources ๋ฉ”์„œ๋“œ๋กœ resource์˜ ํ‘œ์ค€์ ์ธ route(RESTful ์ธํ„ฐํŽ˜์ด์Šค)๊ฐ€ ์ •์˜ ๋œ ๊ฒƒ์ด๋‹ค.

routes ์ •์˜ ํ™•์ธ

  1. rake routes ๋ช…๋ น์–ด๋กœ ํ˜„์žฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ •์˜๋œ route๋ฅผ ๋ชฉ๋ก์œผ๋กœ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  2. ๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:3000/rails/info/routes ๋กœ๋„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Prefix

Verb

URI Pattern

Controller#Action

์—ญํ• 

posts

GET

/posts(.:format)

posts#index

๋ชฉ๋ก ํ™”๋ฉด ํ‘œ์‹œ

POST

/posts(.:format)

posts#create

์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ๋“ฑ๋กํ™”๋ฉด์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„ ๋ฐ์ดํ„ฐ ๋“ฑ๋ก ์ฒ˜๋ฆฌ

new_post

GET

/posts/new(.:format)

posts#new

์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ๋“ฑ๋ก ํ™”๋ฉด ํ‘œ์‹œ

edit_post

GET

/posts/:id/edit(.:format)

posts#edit

์ˆ˜์ • ํ™”๋ฉด ํ‘œ์‹œ

post

GET

/posts/:id(.:format)

posts#show

๊ฐœ๋ณ„ ์ƒ์„ธ ํ™”๋ฉด ํ‘œ์‹œ

PATCH

/posts/:id(.:format)

posts#update

์ˆ˜์ •ํ™”๋ฉด์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ˆ˜์ • ์ฒ˜๋ฆฌ

PUT

/posts/:id(.:format)

posts#update

DELETE

/posts/:id(.:format)

posts#destroy

๋ชฉ๋กํ™”๋ฉด์—์„œ ์ง€์ •๋œ ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ์ฒ˜๋ฆฌ

controller์™€ viewํŒŒ์ผ ์‚ดํŽด๋ณด๊ธฐ

View Helper

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ์˜ ํผ(Form)์€ ์œ ์ € ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•„์ˆ˜์ธ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํผ์˜ ๊ฐ ์š”์†Œ๋“ค์˜ ๋ช…๋ช…๋ฒ•๊ณผ ์ˆ˜๋งŽ์€ ์†์„ฑ๋“ค ํƒ“์— ํผ์˜ ๋งˆํฌ์—…์€ ์‰ฝ๊ฒŒ ๋ณต์žกํ•ด์ง€๊ณ , ๊ด€๋ฆฌํ•˜๊ธฐ ์–ด๋ ค์›Œ์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ Rails์—์„œ๋Š” ํผ ๋งˆํฌ์—…์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ทฐ ํ—ฌํผ๋ฅผ ์ œ๊ณตํ•˜๊ณ , ์ด๋Ÿฐ ๋ฒˆ์žกํ•œ ์ž‘์—…์„ ํ•  ํ•„์š”๋ฅผ ์—†์•ด์Šต๋‹ˆ๋‹ค.

ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์„ ์ž‘์„ฑํ•  ๋•Œ ๋„์›€์„ ์ฃผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋ทฐ ํ—ฌํผ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ž…๋ ฅ ์–‘์‹ ์š”์†Œ ์ƒ์„ฑ์„ ๋น„๋กฏํ•ด ๋ฌธ์ž์—ด ๋˜๋Š” ์ˆซ์ž ์ •ํ˜•ํ™”, ์ธ์ฝ”๋”ฉ ์ฒ˜๋ฆฌ ๋“ฑ ๋ทฐ์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ฒ˜๋ฆฌ๋ฅผ ์†์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ทฐ ํ—ฌํผ์—๋Š” ๋ชจ๋ธ ๋˜๋Š” ๋ผ์šฐํ„ฐ๋ฅผ ์—ฐ๋™ํ•˜๋Š” ๋“ฑ Rails ์ž์ฒด์™€ ํ•จ๊ป˜ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ๋งŽ๋‹ค.

1. index ์•ก์…˜

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end
end

Post.all์€ ํ˜„์žฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์žˆ๋Š” ๋ชจ๋“  ๊ธ€(posts) ์ •๋ณด๋ฅผ Post ๋ชจ๋ธ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ ์ž…๋‹ˆ๋‹ค. ์ด ํ˜ธ์ถœ์˜ ๊ฒฐ๊ณผ๋Š” ๊ธ€(post)์˜ ๋ฐฐ์—ด์ด๊ณ  @posts ๋ณ€์ˆ˜์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

#index.html.erb

<p id="notice"><%= notice %></p>

<h1>Posts</h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Content</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @posts.each do |post| %>
      <tr>
        <td><%= post.title %></td>
        <td><%= post.content %></td>
        <td><%= link_to 'Show', post %></td>
        <td><%= link_to 'Edit', edit_post_path(post) %></td>
        <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Post', new_post_path %>

index ๋ทฐ๋Š” @posts ๋ฐฐ์—ด์„ ์ˆœํ™˜ํ•˜๋ฉด์„œ ๋‚ด์šฉ๊ณผ ๋งํฌ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

view helper

  • link_to(body, url [ , html_options])๋Š” ์„ธ๋ถ€ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ๋งํฌ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

    body : ๋งํฌ ํ…์ŠคํŠธ url : ๋งํฌ ๋Œ€์ƒ ๊ฒฝ๋กœ(or ๋งค๊ฐœ๋ณ€์ˆ˜์ •๋ณด) html_options : ํƒœ๊ทธ์— ์ ์šฉํ•  ์˜ต์…˜์ •๋ณด

  •  <%= link_to 'Show', post %>
  • ์ด ๋ถ€๋ถ„์—์„œ post๋Š” each๋ฉ”์„œ๋“œ์—์„œ @posts๋ณ€์ˆ˜๋กœ ๋ถ€ํ„ฐ ์–ป์€ ๊ฐ๊ฐ์˜ ์š”์†Œ์ด๋‹ค.

  • link_to ๋ฉ”์„œ๋“œ์˜ ๋งํฌ ๋Œ€์ƒ ๊ฒฝ๋กœ์— ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์ ์šฉํ•˜๋ฉด rails๋Š” ๊ฐ์ฒด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” id์†์„ฑ(post.id)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ "/posts/1"๊ณผ ๊ฐ™์€ ๊ฒฝ๋กœ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

  • <%= link_to 'Edit', edit_post_path(post) %>
  • edit_post_path ์™€ new_post_path ๋Š” ๋ ˆ์ผ์ฆˆ๊ฐ€ ์ œ๊ณตํ•˜๋Š” RESTfule ๋ผ์šฐํŒ… ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. resources ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ž๋™์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ทฐ ํ—ฌํผ์ด๋‹ค.

  • <%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
  • data-confirm์˜ต์…˜์„ ์ง€์ •ํ•˜๋ฉด ๋งํฌ๋ฅผ ํด๋ฆญํ•  ๋•Œ ํ™•์ธ ๋Œ€ํ™”์ƒ์ž๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค.

  • HTTP GET์ด์™ธ์˜ ๊ฒƒ์œผ๋กœ ์š”์ฒญ์„ ํ• ๋•Œ method๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. method: :delete๋Š” HTTP DELETE ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

2. Show ์•ก์…˜

#posts_controller.rb

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  def show
  end

  private
    def set_post
      @post = Post.find(params[:id])
    end

end
  • before_action method, only: action ๋Š” action ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „์— ์‹คํ–‰ํ•  ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ ์ž…๋‹ˆ๋‹ค.

    method: ํ•„ํ„ฐ๋กœ ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ action: ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•  ์•ก์…˜ ๋ฉ”์„œ๋“œ

    ์—ฌ๊ธฐ์„œ๋Š” show์•ก์…˜์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— set_post๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

  • private๋Š” ์•ก์…˜์œผ๋กœ ํ˜ธ์ถœ๋˜์ง€ ์•Š๋„๋ก ํ•ด์ค€๋‹ค. ๊ทธ๋ž˜์„œ filter method์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋‚ด์šฉ์„ private์— ์„ ์–ธ๋œ ๊ฒƒ์ด๋‹ค.

  • url์—์„œ ์ „๋‹ฌ๋ฐ›์€ ๋งค๊ฐœ๋ณ€์ˆ˜(id)๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด params method์ด๋ฉฐ ์ด ๋งค๊ฐœ๋ณ€์ˆ˜(id)๋ฅผ ํ‚ค๋กœ Postsํ…Œ์ด๋ธ”์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด findmethod์˜ ์—ญํ• ์ด๋‹ค. ์ฆ‰, set_post๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜(id)๋ฅผ ์ถ”์ถœํ•ด Postsํ…Œ์ด๋ธ”์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ @post์— ์ €์žฅํ•˜๋Š” method์ด๋‹ค.

#show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>

<p>
  <strong>Content:</strong>
  <%= @post.content %>
</p>

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>

view helper

index์™€ ๋™์ผํ•จ.

3. New / Create ์•ก์…˜

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  def new
    @post = Post.new
  end

  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

  private
    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.require(:post).permit(:title, :content)
    end
end

์ž…๋ ฅ ์–‘์‹์„ ์ถœ๋ ฅํ•˜๋Š” new action, ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฐ์ดํ„ฐ ๋“ฑ๋ก ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” create action์ด ์žˆ๋‹ค.

  • new action์€ @post = Post.new ๋น„์–ด์žˆ๋Š” post๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉฐ, ์ด๋•Œ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋กœ ํ…œํ”Œ๋ฆฟํŒŒ์ผ(new.html.erb)์—์„œ ๋ชจ๋ธ์˜ ์†์„ฑ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

  • post_params๋Š” ์ž…๋ ฅ์–‘์‹์œผ๋กœ ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

  • create action์—์„œ post = Post.new(post_params)๋Š” hash๊ฐ’์ด ๋ชจ๋ธ์— ๋Œ€์‘๋˜๋Š” ์†์„ฑ์œผ๋กœ ํ•œ๊บผ๋ฒˆ์— ์„ค์ •์ด๋˜๋ฉฐ, ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ• ๋•Œ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ํ”ผ๋“œ๋ฐฑ ํ•˜๋Š”๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

  • 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
  • respond_to๋ฉ”์†Œ๋“œ๋กœ ์ง€์ •๋œ ํ˜•์‹์œผ๋กœ ํ…œํ”Œ๋ฆฟ์ด ์ถœ๋ ฅ๋˜๋Š” ํ˜•ํƒœ์ด๋‹ค.

    => html์ด๋ผ๋ฉด new.html.erb๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , json์ด๋ผ๋ฉด @post.errors์— JSONํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ์–ด ์ถœ๋ ฅํ•œ๋‹ค.

  • .save ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๊ด€๋ จ๋œ ์ฒ˜๋ฆฌ์˜ ๊ฒฐ๊ณผ๋ฅผ true or false๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์„ฑ์งˆ์„ ์ด์šฉํ•ด์„œ ์ €์žฅ์„ฑ๊ณต์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ณ  ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ์™€ ์•„๋‹Œ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ ์„œ ์ฒ˜๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค.

  • redirect_to url [, option]๋Š” ๋งค๊ฒจ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•œ url๋กœ ์ด๋™ํ•˜๊ฒŒ ๋งŒ๋“  ๊ฒƒ์ด๋‹ค.

#_form.html.erb
<%= form_for(post) do |f| %>
  <% if post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <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 %>
  • ๋ถ€๋ถ„ ํ…œํ”Œ๋ฆฟ์€ ๋ฉ”์ธ ํ…œํ”Œ๋ฆฟ์—์„œ ๋ถˆ๋Ÿฌ๋“ค์ด๋Š” ํ…œํ”Œ๋ฆฟ์ด๋‹ค. _<์ด๋ฆ„>.html.erbํ˜•ํƒœ๋กœ ํŒŒ์ผ์ด๋ฆ„ ์•ž์— _๊ฐ€ ๋ถ™์—ฌ์•ผํ•œ๋‹ค.

    form์—์“ฐ์ธ ์ž…๋ ฅ์–‘์‹์€ ์‹ ๊ทœ๋“ฑ๋ก(new)/์ˆ˜์ •(update)ํŽ˜์ด์ง€์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ๋นผ์„œ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ฝ”๋“œ์˜ ์ค‘๋ณต์ž…๋ ฅ์„ ํ”ผํ•œ ๊ฒƒ์ด๋‹ค.

    view helper

    <%= form_for(post) do |f| %>
    # -------- ์ƒ๋žต -------
    <div class="field">
      <%= f.label :content %>
      <%= f.text_area :content %>
    </div>
    
    <div class="actions">
      <%= f.submit %>
    </div>
    <% end %>
  • ๋ชจ๋ธ๊ณผ ์—ฐ๋™๋˜๋Š” ์ž…๋ ฅ ์–‘์‹์„ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ

    form_for(model) do |f|
      --์ž…๋ ฅ ์–‘์‹--
    end
    1. ์ž…๋ ฅ๊ฐ’์„ ๋ชจ๋ธ์˜ ์†์„ฑ์œผ๋กœ ๋ณ€ํ™˜

    2. ์ˆ˜์ •, ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ• ๋•Œใ… ๋ชจ๋ธ์˜ ํ˜„์žฌ๊ฐ’์„ ์ž…๋ ฅ ์–‘์‹์— ์ถœ๋ ฅ

  • form_for๋ธ”๋ก ๋‚ด๋ถ€์—์„œ f.label, f.text_filed(f.text_area), f.data_select, f.check_box, f.submit๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•จ.

    form for๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ทฐ ํ—ฌํผ๋กœ ๊ฐ๊ฐ ๋ชจ๋ธ๊ณผ ๊ด€๋ จ๋œ ๋ผ๋ฒจ, ํ…์ŠคํŠธ์ƒ์ž, ๋‚ ์งœ ์„ ํƒ ์ƒ์ž, ์ฒดํฌ๋ฐ•์Šค, ์ œ์ถœ๋ฒ„ํŠผ์„ ์ƒ์„ฑํ•œ๋‹ค.

#new.html.erb

<h1>New Post</h1>

<%= render 'form', post: @post %>

<%= link_to 'Back', posts_path %>

<%= render 'form', post: @post %> render๋ฅผ ํ†ตํ•ด์„œ ๋ถ€๋ถ„ํ…œํ”Œ๋ฆฟ ํ˜ธ์ถœ์„ ํ•˜๊ณ  ์žˆ๋‹ค.

4. Edit / Update ์•ก์…˜

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  def edit
  end

  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  private

    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.require(:post).permit(:title, :content)
    end
end

์ˆ˜์ •ํ™”๋ฉด์€ ํŽธ์ง‘ ์ž…๋ ฅ ์–‘์‹์„ ์ถœ๋ ฅํ•˜๋Š” edit์•ก์…˜๊ณผ ๋ฐ์ดํ„ฐ ์ˆ˜์ •์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” update์•ก์…˜์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋‹ค.

  • update๋ฉ”์†Œ๋“œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ๊ฐ’์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•œ๋‹ค. update๋„ save์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ˆ˜์ •์„ฑ๊ณต์—ฌ๋ถ€๋ฅผ true or false๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

#edit.html.erb
<h1>Editing Post</h1>

<%= render 'form', post: @post %>

<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>

์ด ์™ธ์˜ ๊ฒƒ์€ ์•ž์—์„œ ๋‹ค ์„ค๋ช…๋จ

Destroy ์•ก์…˜

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    def set_post
      @post = Post.find(params[:id])
    end

end

destroy ์•ก์…˜์€ id๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ‚ค๋กœ ๊ฐ์ฒด๋ฅผ ์ถ”์ถœํ•˜๊ณ  ์ด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฐ€ํ• ๋•Œ๋Š” .destroy๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

Last updated