rails new scaffold_app
cd scaffold_app
rails g scaffold Post title:string content:text
rake db:migrate
config/routes.rb
Rails.application.routes.draw do
  root "posts#index"
  resources :posts
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
rails g scaffold Post title:string content:text 와 같은 명령어를 실행하면 해당하는 파일을 자동 생성해주며 명령어 하나로 active_record, 경로, 컨트롤러, 뷰파일, helper, jbuilder, assets 등이 자동으로 생성됨active_record
resource_route
scaffold_controller
erb
jbuilder
assets
이와 같이 Model, Controller, View 뿐만 아니라 디자인 요소까지 함께 만들어짐
config/routes.rb
Rails.application.routes.draw do
  root "posts#index"
  resources :posts
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
resources :posts코드로 8개의 주소가 설정됨.RESTapp/controllers/posts_controller.rb
class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
	[...]
  # GET /posts/1
  # GET /posts/1.json
  def show
  end
	
	[...]
	
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end
	
	[...]
end
filter method:set_post 실행:set_post 선언부private
# Use callbacks to share common setup or constraints between actions.
def set_post
	@post = Post.find(params[:id])
end
action method로 생성private 혹은 protected 선언only: [:show, :edit, :update, :destroy]
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 적용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
respond_to do | format | 내부 블록에서 format.type 형식으로 원하는 형식 지정def create
	@post = Post.new(post_params)
	
	[...]
end
[...]
def post_params
	params.require(:post).permit(:title, :content)
end
params.require(:post).permit(:title, :content)params.require(:모델명)으로 먼저 필터링할 모델 설정.permit(:허용할 컬럼 리스트)들을 작성서버가 파라미터를 전달 받은 이후 params.require(:post).permit(:title, :content) 코드로 인해 전달 받은 파라미터 형식 
{title: "안녕", content: "하세요"}로 변경
create action에서 아래와 같이 동작
def create
    @post = Post.new(title: "안녕", content: "하세요")
    
    [...]
end
app/views/posts/new.html.erb
    <h1>New Post</h1>
    
    <%= render 'form', post: @post %>
    
    <%= link_to 'Back', posts_path %>
    ```
    
app/views/posts/edit.html.erb
    <h1>Editing Post</h1>
    
    <%= render 'form', post: @post %>
    
    <%= link_to 'Show', @post %> |
    <%= link_to 'Back', posts_path %>
    ```
    
    - `<=% render 'form', post: @post %>`는 뷰 헬퍼 코드로, 뷰에서 반복되는 부분이 존재할 때 반복되는 부분을 재사용하기 위해 사용
    - 반복되는 부분을 조각 템플릿(partial)로 만든 뒤 이를 메인 템플릿에서 호출하는 방식을 사용
    - prefix → '_'
    - 조각 템플릿 호출 → render 메서드 사용
    `<%= render "조각 파일명" %>`
    - 조각 파일 호출 및 변수 전달
    `<%= render "form", post: @post %>`와 같이 작성
        - post라는 변수에 @post의 값을 담아 _form 조각 파일에 전달
<%= link_to 'Back', posts_path %><a href="/posts">Back</a>Back a tag 감싸질 요소posts_path 클릭 시 이동하는 주소💡 라우트 헬퍼 확인하는 법
- [Terminal]로 확인하기
 
rake routes명령어 실행- Prefix 데이터 → 라우트 헬퍼 확인
 - Rails에서 사용하고 싶다면 끝에
 _path붙여 사용- [브라우저]로 확인
 
- 주소/rails/info/routes로 접속
 
app/views/posts/index.html.erb
   <% @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 %>
<td><%= link_to 'Show', post %></td>
post/posts/:id 주소로 이동<td><%= link_to 'Edit', edit_post_path(post) %></td>
edit_post_path(post)는 edit_post_path에서 :id 라우트 파라미터르 받아옴edit_post_path(post.id)로 작성 가능app/views/posts/_form.html.erb    <%= form_with(model: post, local: true) do |form| %>
      <% 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">
        <%= form.label :title %>
        <%= form.text_field :title %>
      </div>
    
      <div class="field">
        <%= form.label :content %>
        <%= form.text_area :content %>
      </div>
    
      <div class="actions">
        <%= form.submit %>
      </div>
    <% end %>
form_with
form_authenticate_token과 같이 Rails에서 form을 사용할 때 필요한 설정을 자동 설정form_with(model: post, local: true)
Post.new, edit 액션 → Post.find(params[:id])Reference