「確認画面」ベストプラクティス

 「確認画面」ベストプラクティス

RailsでWebアプリケーションを作るとほぼ必ず要件として現れる「入力確認画面」。しかしRailsのテンプレートとしては用意されていないので、プロジェクトごとに意見が分かれる部分の1つではないでしょうか。この発表では実際のプロジェクトで起きた事例を通じて、入力確認画面の実装を比較・紹介し、ベストプラクティスのノウハウを共有します。

48783fcaf907ac381b37ecc33ed9e761?s=128

strviola

July 17, 2019
Tweet

Transcript

  1. ʮ֬ೝը໘ʯ ϕετϓϥΫςΟε Linkers, inc. ླ໦ཽଠ 1

  2. 2

  3. ࣗݾ঺հ ླ໦ཽଠ (@strviola) ϦϯΧʔζגࣜձࣾ Ruby on Rails, AWS, JS ࠷ۙҾͬӽ͠·ͨ͠

    3
  4. 4

  5. 5 https:/ /linkers.net ΑΓ

  6. 6 https:/ /linkers.net ΑΓ

  7. 7 https:/ /linkers.net ΑΓ

  8. 8 https:/ /linkers.net ΑΓ

  9. BtoB ϏδωεϚονϯάγεςϜ ৽ن༻్ల։౳Ͱച্ٸ֦େͷ༨஍͕େ ͖͍اۀΛධՁ 9

  10. ຊ୊ Bad Practice ˠ Best Practice ˠ ·ͱΊ 10

  11. ֬ೝը໘ https:/ /techplay.jp ΑΓ 11

  12. ֬ೝը໘ https:/ /techplay.jp ΑΓ 12

  13. ཁ݅ ϑΥʔϜೖྗ஋͕ valid ͳΒ֬ೝը໘ʹભҠ ϑΥʔϜೖྗ஋Λදࣔ "֬ఆ" ΛΫϦοΫͯ͠ POST "मਖ਼" ΛΫϦοΫͯ͠ϑΥʔϜʹ໭Δ

    "मਖ਼" Ͱ໭ͬͨࡍʹೖྗ஋Λอଘ ϑΥʔϜೖྗ஋͕ valid Ͱͳ͚Ε͹ϑΥʔϜΛ࠶දࣔ 13
  14. Bad Practice 14

  15. গ͠લͷฐࣾ module ConfirmModule extend ActiveSupport::Concern included do validates :confirmed, acceptance:

    true after_validation :check_confirming end def check_confirming errors.delete(:confirmed) self.confirmed = errors.empty? ? '1' : '' end def clear_confirmed self.confirmed = '' end end 15 ͨͩ͠νΣοΫϘοΫεແ͠
  16. গ͠લͷฐࣾ class Message < ApplicationRecord include ConfirmModule validates :body, presence:

    true end 16
  17. গ͠લͷฐࣾ class MessagesController < ApplicationController def create @message = Message.new(message_params)

    if params[:fix] @message.clear_confirmed render :new elsif @message.save redirect_to @message else render :new end end end 17 ʮमਖ਼ʯΫϦοΫͨ͠ ϑΥʔϜ͕ଥ౰ ೖྗϛε or ֬ೝ "or" ?
  18. গ͠લͷฐࣾ / new.html.slim - if @message.confirmed.blank? h1 New Message =

    render 'form', message: @message - else h1 Confirm Message = render 'confirm', message: @message = link_to 'Back', messages_path 18 ࠷ॳ or Τϥʔ ! blank ֬ೝը໘ ! present ϑΥʔϜදࣔ ֬ೝը໘
  19. গ͠લͷฐࣾ / _form.html.slim = form_for message do |form| - if

    message.errors.any? / display errors = form.hidden_field :confirmed = form.text_area :body = form.submit '֬ೝ' 19 '' (blank)
  20. গ͠લͷฐࣾ / _confirm.html.slim = form_for message do |form| = form.hidden_field

    :confirmed = form.hidden_field :body h2 body p = message.body = form.submit 'ૹ৴' = form.submit 'मਖ਼', name: 'fix' 20 ֬ೝ಺༰දࣔ '1' (present) params[:fix].present? == true
  21. গ͠લͷฐࣾ class MessagesController < ApplicationController def create @message = Message.new(message_params)

    if params[:fix] @message.clear_confirmed render :new elsif @message.save redirect_to @message else render :new end end end 21 ֬ೝը໘͔Βˠ message[confirmed] = '1' ඞཁʹԠͯ͡ফ͢
  22. build @model @model.valid? .check_confirming .confirmed params['fix'] render :new .save! redirect

    POST :create true false blank? present? present? blank? .clear_confirmed 22
  23. 23

  24. ͠ΜͲ͔ͬͨॴ ௚ײతͰͳ͍ new.html.slim ͕Ͱ͔͍
 ໾ׂ͕ॏͳͷͰ͞΋͋ΓͳΜ ʮ͓໿ଋʯ͕ࢄΒ͹Δ
 (model, view, controller) 24

  25. ṖͷΤϥʔ 25

  26. গ͠લͷฐࣾ class MessagesController < ApplicationController def create @message = Message.new(message_params)

    @message.validate Rails.logger.debug @message.errors if params[:fix] @message.clear_confirmed render :new elsif @message.save redirect_to @message else render :new end end end 26
  27. build @model @model.valid? .check_confirming .confirmed params['fix'] render :new .save! redirect

    POST :create true false blank? present? present? blank? .clear_confirmed 27 confirmed = '1'
  28. 28

  29. Best Practice (ࠓͷॴ…) 29

  30. Confirm Action Rails.application.routes.draw do resources :users do post :confirm, on:

    :collection end end 30
  31. Confirm Action / new.html.slim h1 | New User = form_for

    @user, url: { action: :confirm } do |form| = render 'form', user: @user, form: form = link_to 'Back', users_path 31
  32. Confirm Action class UsersController < ApplicationController def confirm @user =

    User.new(user_params) if @user.valid? render :confirm else flash.now[:error] = "Validation error" render :new end end # continue 32
  33. Confirm Action / confirm.html.slim h1 Confirm = form_for @user, url:

    { action: :create } do |f| = f.hidden_field :name h2 Name p = @user.name = f.submit 'ૹ৴' = f.submit 'मਖ਼', name: 'fix' 33
  34. Confirm Action def create @user = User.new(user_params) if params['fix'] render

    :new elsif @user.save redirect_to @user else flash.now[:error] = "Validation error" render :new end end end 34 ʮमਖ਼ʯ OK NG (ແཧ͠ͳ͍ͱى͖ͳ͍)
  35. build @user @user.valid? render :confirm @user.valid? render :new (΍Γ௚͠) @user.save!

    redirect POST :create 35 POST :confirm params['fix'] true false present? blank? false true
  36. build @model @model.valid? .check_confirming .confirmed params['fix'] render :new .save! redirect

    POST :create true false blank? present? present? blank? .clear_confirmed 36 (࠶ܝ)
  37. շదͳॴ ϞσϧଆΛԚ͞ͳͯ͘ࡁΉ Ͳ͜Ͱ validate ͯ͠΋ಈ͘ʂ View ͷઃܭ͕γϯϓϧʂ 37

  38. ΠϚΠνͳॴ ϝιουɺϑΝΠϧɺߦ਺͸૿͑Δ هड़͕ॏෳ͕ͪ͠ create ͱ update ͷڞଘ͕͠ΜͲ͍ 38 resources :user

    do post :confirm, on: :collection post :update_confirm, on: :member end
  39. ༨ஊ Rails ඪ४ʹແ͍ཧ༝ ʮͦΜͳ΋Μ͍ΒΜʯ ೔ຊਓ͸ಛʹ޷͖͔΋ 39

  40. Rails Way ͔Β֎ΕΔ == ͭΒ͍ 40

  41. ·ͱΊ ·ͣɺ֬ೝը໘͸ຊ౰ʹඞཁ͔ʁ confirm ΞΫγϣϯ͸෼͚Α͏ Ϟσϧʹ֬ೝը໘༻ͷ࣮૷͸ೖΕͳ͍ 41

  42. Nous recrutons! ΤϯδχΞ / σβΠφʔ ϑϨοΫεۈ຿੍౓͋Γ ෱རްੜ ษڧձ 42 ࢲ͕ߨࢣͰ͢ʂ