2020.8.7の学習ノート
◆サマリ
- 日記アプリの登録処理の実装と流れをチェックする
- ActiveRecordの内部的なバリデーションについて
- Dockerでの環境構築について
◆フォーム処理の内部フロー
newアクション
app/controller/diaries_controller
対応ビュー
app/views/diaries/new.html.erb
<h1>New Diary</h1>
<%= render 'form', diary: @diary %>
<%= link_to 'Back', diaries_path %>
renderメソッド->部分テンプレートのコール
app/views/diaries/_form.html.erb
呼び出された<form_with>ビューヘルパー
<%= form_with(model: diary, local: true) do |form| %>
<% if diary.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(diary.errors.count, "error") %> prohibited this diary from being saved:</h2>
<ul>
<% diary.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 :body %>
<%= form.text_area :body %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
<label>ビューヘルパー
オブジェクトに最適化されたタグを出力する
<%= form.label :title %>
<%= form.text_field :title %>
<text_area>ビューヘルパー
オブジェクトに最適化されたタグを出力する
<%= form.label :body %>
<%= form.text_area :body %>
<submit>ビューヘルパー
オブジェクトに最適化されたタグを出力する
<div class="actions">
<%= form.submit %>
◆データ登録時の処理に関わるファイル
app/controllers/diaries_controller.rb
class DiariesController < ApplicationController
before_action :set_diary, only: [:show, :edit, :update, :destroy]
# GET /diaries
# GET /diaries.json
def index
@diaries = Diary.all
end
# GET /diaries/1
# GET /diaries/1.json
def show
end
# GET /diaries/new
def new
@diary = Diary.new
end
# GET /diaries/1/edit
def edit
end
# POST /diaries
# POST /diaries.json
def create
@diary = Diary.new(diary_params)
respond_to do |format|
if @diary.save
format.html { redirect_to @diary, notice: 'Diary was successfully created.' }
format.json { render :show, status: :created, location: @diary }
else
format.html { render :new }
format.json { render json: @diary.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /diaries/1
# PATCH/PUT /diaries/1.json
def update
respond_to do |format|
if @diary.update(diary_params)
format.html { redirect_to @diary, notice: 'Diary was successfully updated.' }
format.json { render :show, status: :ok, location: @diary }
else
format.html { render :edit }
format.json { render json: @diary.errors, status: :unprocessable_entity }
end
end
end
# DELETE /diaries/1
# DELETE /diaries/1.json
def destroy
@diary.destroy
respond_to do |format|
format.html { redirect_to diaries_url, notice: 'Diary was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_diary
@diary = Diary.find(params[:id])
end
# Only allow a list of trusted parameters through.
def diary_params
params.require(:diary).permit(:title, :body)
end
end
Strong paramater
# POST /diaries
# POST /diaries.json
def create
@diary = Diary.new(diary_params)
respond_to do |format|
if @diary.save
format.html { redirect_to @diary, notice: 'Diary was successfully created.' }
format.json { render :show, status: :created, location: @diary }
else
format.html { render :new }
format.json { render json: @diary.errors, status: :unprocessable_entity }
end
end
end
private_methodであるdiary_paramメソッドの戻り値をインスタンス化している。
→入力フォームの値をハッシュとして返す。
diary_paramメソッド
フォームのデータをparamメソッドで取得している。
require/permitで、データを指定フォーマットで受け取っている。
このしくみをStrong Paramaterという。
requireではモデル名のハッシュを指定、permitでは列名(ハッシュ内のキー)を指定
params.require(:diary).permit(:title, :body)
具体的には以下のハッシュが返される
{
'title': '今日は山の日'
'body': '山の日は祝日です'
}
respond_to
アクセスURLの末尾の拡張子によって処理を分ける
- HTML
- JSON
日記データをsaveメソッドでテーブルに保存する。
保存後にredirect_toメソッドが呼び出される。
引数指定だが、メッセージとリダイレクトのリンクを定義する。
JSON形式での処理
この場合、renderにshowがあるので、
app/views/diaries/show.json.jbuilderがコール
json.partial! "diaries/diary", diary: @diary
Jbuilderの部分テンプレートが呼ばれる
json.partial!
呼び出している箇所_diary.json.jbuilderをみる
json.extract! diary, :id, :title, :body, :created_at, :updated_at
json.url diary_url(diary, format: :json)
json.extract!によってキーと値のペアをハッシュで出力する(=json形式)
◆データの更新処理について
PATCH処理(=データ更新命令)をオーダーする。
内部的な違い
- モデルインスタンス自体ではなく、モデルインスタンスのidを代入する
- ActiveRecordのsaveではなくupdateをよぶ
- flashメッセージがcreateではなくupdate
- 更新した日記データに強制リダイレクトする
◆データ削除処理について
物理削除と論理削除
れこーどから消す物理削除、
レコードにカラムを追加してそこの値が1ならば削除とみなす論理削除
→テーブルからレコードを削除せず特定のルールに従って削除扱いにすること
◆検証機能の実装
◆空データでない検証ルールを追加する
validatesメソッドを利用
app/models/diary.rb
class Diary < ApplicationRecord
validates :title, presence: true
validates :body, presence: true
end
長さ上限ルール