Scaffolding
scaffolding์ด๋ '๊ธฐ๋ฐ'์ด๋ผ๋ ์๋ฏธ๋ก, ๊ธฐ๋ณธ ๊ธฐ๋ฅ์ ๋ฏธ๋ฆฌ ๊ตฌํํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ณจ๊ฒฉ(๊ธฐ๋ฐ)์ ์์ฑํ๊ธฐ ์ํ ๊ธฐ๋ฅ
1. rails project ์์ฑ
rails new learn_project2. 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.scssscaffold๋ช ๋ น์ด๋ฅผ ํตํด์ ์๋์ผ๋ก ์์ฑ๋ ํ์ผ๋ค ์ด๋ค. ์ด ์ค์ 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
URL์ ํด๋นํ๋ ์๋ฃ์ ์ ์ก์ ์์ฒญํ๋ค.
๋ฐ์ดํฐ๊ฐ URL์ ๋ ธ์ถ๋๋ค.
์ธ์ฝ๋ฉ/๋์ฝ๋ฉ์ ๊ณผ์ ์ด ์๊ธฐ ๋๋ฌธ์ POST๋ณด๋ค ๋น ๋ฅด๋ค.
URL์ ๊ธธ์ด ์ ์ฝ์ผ๋ก ์ธํด ๋ง์ ๋ฐ์ดํฐ ์ ์ก์ ๋ฌด๋ฆฌ์ด๋ค.
POST
์๋ฒ๊ฐ ์ฒ๋ฆฌํ ์ ์๋ ์๋ฃ๋ฅผ ๋ณด๋ธ๋ค.
๋ฐ์ดํฐ๋ HTTP Boby์ ์จ๊ฒจ์ ์๋ฒ๋ก ์ ์กํ๋ค.
GET์ผ๋ก ๋ณด๋ผ์ ์๋ ์๋ฃ๋ฅผ ์ ์กํ ๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
HEAD
GET๊ณผ ๊ฐ์ ์์ฒญ์ด์ง๋ง, ์น ์๋ฒ์์ ํค๋ ์ ๋ณด ์ด์ธ์๋ ์ด๋ค ๋ฐ์ดํฐ๋ ๋ณด๋ด์ง ์๋๋ค.
์น ์๋ฒ์ ๋ค์ด ์ฌ๋ถ ์ ๊ฒ(Health Check)์ด๋ ์น ์๋ฒ ์ ๋ณด(๋ฒ์ ๋ฑ)๋ฑ์ ์ป๊ธฐ ์ํด ์ฌ์ฉ๋ ์ ์๋ค.
OPTIONS
์๋ฒ๊ฐ ํน์ URL์ ๋ํด ์ด๋ ํ HTTP Method๋ฅผ ์ง์ํ๋์ง ๋ฌป๋๋ค.
PUT
ํด๋น URL์ ์๋ฃ๋ฅผ ์ ์ฅํ๋ค.(POST์ ์ ์ฌํ ์ ์ก ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ํค๋ ์ด์ธ์ ๋ฉ์์ง(๋ฐ์ดํฐ)๊ฐ ํจ๊ป ์ ์ก๋๋ค.)
์๊ฒฉ์ง ์๋ฒ์ ์ง์ ํ ์ฝํ ์ธ ๋ฅผ ์ ์ฅํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ฉฐ ํํ์ด์ง ๋ณ์กฐ์ ๋ง์ด ์ ์ฉ๋๊ณ ์๋ค.
DELETE
ํด๋น URL์ ์๋ฃ๋ฅผ ์ญ์ ํ๋ค.
TRACE
์ด์ ์ ์์ฒญํ ๋ด์ฉ์ ๋ค์ ๊ฒ์ ์์ฒญํ๋ค.
์๊ฒฉ์ง ์๋ฒ์ Loopback(๋ฃจํ๋ฐฑ) ๋ฉ์์ง๋ฅผ ํธ์ถํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ค.
CONNECT
ํ๋ก์๊ฐ ์ฌ์ฉํ๋ ์์ฒญ.
PATCH
๋ฆฌ์์ค์ ๋ํ ๋ถ๋ถ์ ์ธ ์์ ์ ์ ์ฉํ๋ค.
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
endresources ๋ฉ์๋๋ก resource์ ํ์ค์ ์ธ route(RESTful ์ธํฐํ์ด์ค)๊ฐ ์ ์ ๋ ๊ฒ์ด๋ค.
routes ์ ์ ํ์ธ
rake routes ๋ช ๋ น์ด๋ก ํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ ์๋ route๋ฅผ ๋ชฉ๋ก์ผ๋ก ํ์ธ ํ ์ ์๋ค.
๋ธ๋ผ์ฐ์ ์์ 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
endPost.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
endbefore_action method, only: action๋ action ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ ์ ์ ์คํํ ๋ฉ์๋๋ฅผ ์ง์ ํ๋ ๋ฉ์๋ ์ ๋๋ค.method: ํํฐ๋ก ์คํ๋๋ ๋ฉ์๋ action: ํํฐ๋ฅผ ์ ์ฉํ ์ก์ ๋ฉ์๋
์ฌ๊ธฐ์๋ show์ก์ ์ด ์คํ๋๊ธฐ ์ ์ set_post๊ฐ ์คํ๋ฉ๋๋ค.
private๋ ์ก์ ์ผ๋ก ํธ์ถ๋์ง ์๋๋ก ํด์ค๋ค. ๊ทธ๋์ filter method์์ ์ฒ๋ฆฌํ๋ ๋ด์ฉ์ private์ ์ ์ธ๋ ๊ฒ์ด๋ค.
url์์ ์ ๋ฌ๋ฐ์ ๋งค๊ฐ๋ณ์(id)๋ฅผ ์ถ์ถํ ์ ์๊ฒ ํ๋ ๊ฒ์ด
paramsmethod์ด๋ฉฐ ์ด ๋งค๊ฐ๋ณ์(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 endrespond_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์ ๋ ฅ๊ฐ์ ๋ชจ๋ธ์ ์์ฑ์ผ๋ก ๋ณํ
์์ , ์ค๋ฅ๊ฐ ๋ฐ์ํ ๋ใ ๋ชจ๋ธ์ ํ์ฌ๊ฐ์ ์ ๋ ฅ ์์์ ์ถ๋ ฅ
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
enddestroy ์ก์
์ id๋งค๊ฐ๋ณ์๋ฅผ ํค๋ก ๊ฐ์ฒด๋ฅผ ์ถ์ถํ๊ณ ์ด๋ฅผ ์ ๊ฑฐํ๋ค. ๋ฐ์ดํฐ๋ฅผ ์ ๊ฐํ ๋๋ .destroy๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
Last updated
Was this helpful?