Scaffolding
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
scaffoldingμ΄λ 'κΈ°λ°'μ΄λΌλ μλ―Έλ‘, κΈ°λ³Έ κΈ°λ₯μ 미리 ꡬνν μ ν리μΌμ΄μ μ 골격(κΈ°λ°)μ μμ±νκΈ° μν κΈ°λ₯
rails new learn_project
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νμΌλ€μ μ΄ν΄ λ³Ό κ²μ΄λ€.
HTTPλ HyperText Transport Protocolμ μ½μλ‘ μΉμλ²μ ν΄λΌμ΄μΈνΈκ°μ λ¬Έμλ₯Ό κ΅ννκΈ° μν΄ μ¬μ©λλ νλ‘ν μ½μ΄λ€.
World Wide Web( WWW )μ λΆμ°λμ΄ μλ Serverμ Client κ°μ Hypertextλ₯Ό μ΄μ©ν μ 보κ΅νμ΄ κ°λ₯νλλ‘ νλ ν΅μ κ·μ½μ΄λ€.
νμ΄νΌν μ€νΈλ λ¬Έμ μ€κ°μ€κ°μ νΉμ ν€μλλ₯Ό λκ³ λ¬Έμλ κ·Έλ¦Όμ μνΈ μ κΈ°μ μΌλ‘ κ²°ν©νμ¬ μ°κ²°μν΄μΌλ‘μ¨, μλ‘ λ€λ₯Έ λ¬ΈμλΌ ν μ§λΌλ νλμ λ¬ΈμμΈ κ²μ²λΌ 보μ΄λ©΄μ μ°Έμ‘°νκΈ° μ½λλ‘ νλ λ°©μμ μλ―Ένλ€.
μΈν°λ· μ£Όμλ₯Ό μ§μ ν λ 'http://www....' μ κ°μ΄ νλ κ²μ wwwλ‘ μμλλ μΈν°λ· μ£Όμμμ νμ΄νΌν μ€νΈ λ¬Έμμ κ΅νμ http ν΅μ κ·μ½μΌλ‘ μ²λ¦¬νλΌλ λ»μ΄λ€.
httpμ 첫λ²μ§Έ λ²μ μ μΈν°λ·μ ν΅νμ¬ κ°κ³΅λμ§ μμ λ°μ΄ν°λ₯Ό μ μ‘νκΈ° μν λ¨μν νλ‘ν μ½μ΄μμΌλ, λ°μ΄ν°μ λν μ μ‘κ³Ό μꡬ·μλ΅μ λν μμ λ± κ°κ³΅λ μ 보λ₯Ό ν¬ν¨νλ νλ‘ν μ½λ‘ κ°μ λμλ€.
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
리μμ€μ λν λΆλΆμ μΈ μμ μ μ μ©νλ€.
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μΈμ§λ₯Ό μ΄ν΄ ν μ μλ μ체 νν ꡬ쑰λ₯Ό κ°μ§λ€.
routes.rb
Rails.application.routes.draw do
resources :posts
end
resources λ©μλλ‘ 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
μΉ μ ν리μΌμ΄μ μμμ νΌ(Form)μ μ μ μ λ ₯μ λ°κΈ° μν΄μλ νμμΈ μΈν°νμ΄μ€μ λλ€. νμ§λ§ νΌμ κ° μμλ€μ λͺ λͺ λ²κ³Ό μλ§μ μμ±λ€ νμ νΌμ λ§ν¬μ μ μ½κ² 볡μ‘ν΄μ§κ³ , κ΄λ¦¬νκΈ° μ΄λ €μμ§λλ€. κ·Έλμ Railsμμλ νΌ λ§ν¬μ μ μμ±νκΈ° μν λ·° ν¬νΌλ₯Ό μ 곡νκ³ , μ΄λ° λ²μ‘ν μμ μ ν νμλ₯Ό μμ΄μ΅λλ€.
ν νλ¦Ώ νμΌμ μμ±ν λ λμμ μ£Όλ λ©μλλ₯Ό μλ―Ένλ€. λ·° ν¬νΌλ₯Ό μ΄μ©νλ©΄ μ λ ₯ μμ μμ μμ±μ λΉλ‘―ν΄ λ¬Έμμ΄ λλ μ«μ μ νν, μΈμ½λ© μ²λ¦¬ λ± λ·°μμ μμ£Ό μ¬μ©λλ μ²λ¦¬λ₯Ό μμ½κ² ν μ μμ΅λλ€. λν λ·° ν¬νΌμλ λͺ¨λΈ λλ λΌμ°ν°λ₯Ό μ°λνλ λ± Rails μ체μ ν¨κ» μ΄μ©ν μ μλ κΈ°λ₯μ΄ λ§λ€.
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"κ³Ό κ°μ κ²½λ‘κ° μμ±λλ€.
edit_post_path μ new_post_path λ λ μΌμ¦κ° μ 곡νλ RESTfule λΌμ°ν λΆλΆμ λλ€. resources λ©μλλ₯Ό νΈμΆν λ μλμΌλ‘ μ¬μ©λλ λ·° ν¬νΌμ΄λ€.
data-confirmμ΅μ μ μ§μ νλ©΄ λ§ν¬λ₯Ό ν΄λ¦ν λ νμΈ λνμμκ° νμλλ€.
HTTP GETμ΄μΈμ κ²μΌλ‘ μμ²μ ν λ methodλ‘ μ§μ ν μ μλ€. method: :deleteλ HTTP DELETE λ©μλλ₯Ό μ¬μ©νλ€λ κ²μ΄λ€.
#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ν
μ΄λΈμ κ²μνλ κ²μ΄ find
methodμ μν μ΄λ€. μ¦, 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μ λμΌν¨.
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
λ©μλλ‘ μ§μ λ νμμΌλ‘ ν
νλ¦Ώμ΄ μΆλ ₯λλ ννμ΄λ€.
=> 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λ₯Ό ν΅ν΄μ λΆλΆν
νλ¦Ώ νΈμΆμ νκ³ μλ€.
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 %>
μ΄ μΈμ κ²μ μμμ λ€ μ€λͺ λ¨
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
λ©μλλ₯Ό μ¬μ©νλ€.
μλ‘μ΄ λ°μ΄ν° λ±λ‘νλ©΄μ μ λ ₯μ λ°μ λ°μ΄ν° λ±λ‘ μ²λ¦¬
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
λͺ©λ‘νλ©΄μμ μ§μ λ λ°μ΄ν° μ κ±°μ²λ¦¬
<%= link_to 'Edit', edit_post_path(post) %>
<%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
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