Upgrade to Pro — share decks privately, control downloads, hide ads and more …

cookpad-17day-tech-internship-2017-rails

 cookpad-17day-tech-internship-2017-rails

クックパッド17day技術インターンシップの前半、Railsへんの講義資料です

MOROHASHI Kyosuke

August 31, 2017
Tweet

More Decks by MOROHASHI Kyosuke

Other Decks in Programming

Transcript

  1. Web & Rails
    2017-08-08
    @moro ॾڮګհ

    View full-size slide

  2. ࢀߟ৘ใ:
    ͖ΐ͏͸ྔ͕ଟ͍ͷͰ

    ͋ͱ͔ΒৼΓฦΔͨΊͷࢿྉ

    View full-size slide


  3. https://railstutorial.jp/

    View full-size slide


  4. https://www.railstutorial.org/
    ':*ӳޠͷݪஶ΋͋ΔΑ

    View full-size slide


  5. ':*ͪ͜Β΋ӳޠ൛͋Γ·͢ɻ։ൃ൛ FEHF
    ͷΨΠυ΋͋Δɻ

    View full-size slide

  6. ‣ RailsΛ࢖ͬͯ؆୯ͳWebΞϓϦέʔγϣϯΛ࡞Δɻ
    ‣ ͦΕΛ௨ͯ͡ɺWebΞϓϦέʔγϣϯʹඞཁͳཁૉ
    ٕज़ʹ;ΕΔɻ
    ‣ REST: αʔϏεͷཁૉΛʮϦιʔεͷಡΈॻ͖ʯͱந৅ԽͰ͖Δ
    ‣ DBઃܭ: ਖ਼نԽͨ͠σʔλΛARܦ༝Ͱ͞ΘΔ
    ‣ ηογϣϯ: HTTPͰΫϥΠΞϯτঢ়ଶΛѻ͏ఆ൪Λ஌Δ
    ࠓ೔ͷΰʔϧ

    View full-size slide

  7. ͳ͍ͥ·Web͔

    View full-size slide

  8. ‣ εϚϗΞϓϦΛ௨ͯ͡Ձ஋Λಧ͚ΔͨΊͷʮ;ͭ͏ʯͷ

    ܦ࿏ͱͯ͠ɺJSON over HTTPͳAPI͕͋Δɻ
    ‣ ػցֶशetc… ͷ੒ՌΛϢʔβʹಧ͚Δ৔߹ʹ΋

    Webٕज़Λհ͢Δ͜ͱ͕ଟ͍ɻ
    ‣ Webͦͷ΋ͷ΋·ͩ·ͩ໘ന͍ (ࢲ͸ίί)
    ‣ ϏδωεϩδοΫΛॻ͖΍͍͢͠Ͷɻ
    ͳ͍ͥ·8FC͔

    View full-size slide

  9. ‣ Railsͱ͸ɺRubyϓϩάϥϛϯάݴޠͰॻ͔ΕͨWebΞϓϦ
    έʔγϣϯϑϨʔϜϫʔΫͰ͢ɻRails͸ɺ͋ΒΏΔ։ൃऀ
    ͕WebΞϓϦέʔγϣϯͷ։ൃΛ࢝ΊΔ͏͑ͰඞཁͱͳΔ
    ࡞ۀ΍ϦιʔεΛࣄલʹԾఆͯ͠४උ͓ͯ͘͜͠ͱͰɺ
    WebΞϓϦέʔγϣϯΛΑΓ؆୯ʹϓϩάϥϛϯάͰ͖Δ
    Α͏ʹઃܭ͞Ε͍ͯ·͢ɻ
    ‣ -- http://railsguides.jp/getting_started.html

    View full-size slide

  10. ‣ Railsͱ͸ɺRubyϓϩάϥϛϯάݴޠͰॻ͔ΕͨWebΞϓϦ
    έʔγϣϯϑϨʔϜϫʔΫͰ͢ɻRails͸ɺ͋ΒΏΔ։ൃऀ
    ͕WebΞϓϦέʔγϣϯͷ։ൃΛ࢝ΊΔ͏͑ͰඞཁͱͳΔ
    ࡞ۀ΍ϦιʔεΛࣄલʹԾఆͯ͠४උ͓ͯ͘͜͠ͱͰɺ
    WebΞϓϦέʔγϣϯΛΑΓ؆୯ʹϓϩάϥϛϯάͰ͖Δ
    Α͏ʹઃܭ͞Ε͍ͯ·͢ɻ
    ‣ -- http://railsguides.jp/getting_started.html

    View full-size slide

  11. WebΞϓϦέʔγϣϯͱ
    HTTP

    View full-size slide

  12. ‣ Hypertext Transfer Protocol
    ‣ ෼ࢄίϯϐϡʔλؒͰHTMLͳͲͷ৘ใΛ΍ΓͱΓ͢ΔͨΊͷϓϩτίϧɻ
    ‣ ΫϥΠΞϯτ(ϒϥ΢βɺεϚϗΞϓϦɺผͷαʔόΞϓϦͳͲ)͕ɺ
    αʔό্ͷʮԿ͔ͷ৘ใʯΛʮͲ͏ͯ͠΄͍͠ʯͱ
    ཁٻ(ϦΫΤετ)͠ɺαʔό͕Ԡ౴(Ϩεϙϯε)͢
    Δɻ
    )551

    View full-size slide

  13. ‣ Կ͔ͷ৘ใ: ύεͱΫΤϦ
    ‣ Ͳ͏ͯ͠΄͍͠: HTTPϝιου
    ʮԿ͔ͷ৘ใʯΛʮͲ͏ͯ͠΄͍͠ʯ

    View full-size slide

  14. [0]$ telnet cookpad.com 80
    Trying 54.199.213.92...
    Connected to cookpad.com.
    Escape character is '^]'.
    GET / HTTP/1.1
    Host: cookpad.com
    HTTP/1.1 301 Moved Permanently
    Content-Type: text/html; charset=iso-8859-1
    Date: Wed, 22 Feb 2017 07:29:24 GMT
    Location: https://cookpad.com/
    Server: Apache
    Content-Length: 228
    Connection: keep-alive


    301 Moved Permanently

    )551

    View full-size slide

  15. ‣ GET : Ͳ͏͢Δ
    ‣ ʮHTTPϝιουʯ
    ‣ GET=औಘɺPOST=࡞੒ɺPATCH=(Ұ෦)ஔ͖׵͑ɺDELETE=࡟আ
    ‣ / : ͳʹΛ
    ‣ ͜ͷ৔߹ɺhttp://cookpad.com ͷτοϓϖʔδ
    (&5

    View full-size slide

  16. ‣ HTTP/1.1
    ‣ ௨৴͢ΔHTTPͷόʔδϣϯΛද͢ɻ޿͘࢖ΘΕ͍ͯΔͷ͸1.1
    ‣ ݱ୅తͳ໰୊Λղܾ͢ΔͨΊHTTP/2΋ग़͖ͯͨ
    ‣ GET / HTTP/1.1 ͷߦΛʮϦΫΤετϥΠϯʯͱݺͿ
    ‣ Host: cookpad.com
    ‣ 1αʔόͰෳ਺αʔϏεΛӡ༻Ͱ͖ΔͨΊɺ࿦ཧతʹͲͷαʔϏε΁ͷϦΫΤετͰ͋Δ͔Λࣔ͢ඞཁ͕
    ͋Δ
    ‣ ۭߦ
    ‣ ϦΫΤετ͕ऴΘͬͨ΋ͷͱͯ͠αʔόͷॲཧ͕࢝·Δ
    ͦͷଞͷՕॴ

    View full-size slide

  17. ‣ HTTPͰϦΫΤετ͏͚ɺαʔό্ͰͳΜΒ͔ͷॲཧ
    Λߦ͍ɺϨεϙϯεΛฦ͢
    ‣ τοϓϖʔδ(/)͕΄͍͠(GET)ͱ͍͏ϦΫΤετʹͨ
    ͍ͯ͠ɺτοϓϖʔδͷHTMLΛੜ੒ͯ͠Ϩεϙϯε
    ͢Δͷ͕WebΞϓϦέʔγϣϯ
    8FCΞϓϦέʔγϣϯ

    View full-size slide

  18. ‣ Railsͱ͸ɺRubyϓϩάϥϛϯάݴޠͰॻ͔ΕͨWebΞϓϦ
    έʔγϣϯϑϨʔϜϫʔΫͰ͢ɻRails͸ɺ͋ΒΏΔ։ൃऀ
    ͕WebΞϓϦέʔγϣϯͷ։ൃΛ࢝ΊΔ͏͑ͰඞཁͱͳΔ
    ࡞ۀ΍ϦιʔεΛࣄલʹԾఆͯ͠४උ͓ͯ͘͜͠ͱͰɺ
    WebΞϓϦέʔγϣϯΛΑΓ؆୯ʹϓϩάϥϛϯάͰ͖Δ
    Α͏ʹઃܭ͞Ε͍ͯ·͢ɻ
    ‣ -- http://railsguides.jp/getting_started.html

    View full-size slide

  19. ඞཁͳϞϊΛԾఆͯ͠४උ

    ϑϧελοΫϑϨʔϜϫʔΫ

    View full-size slide

  20. ‣ Railsʹ͸ҰൠతͳWebΞϓϦέʔγϣϯʹඞཁͳػೳ͕

    Ұἧ͑ೖ͍ͬͯΔɻ
    ‣ HTTPϦΫΤετ͔ΒRubyͷॲཧΛݺͼग़͢͠ɺΫϥΠΞϯτʹ݁ՌΛ

    ฦͨ͢Ίͷ࢓૊Έ
    ‣ DBͷσʔλΛಡΈॻ͖͢Δ࢓૊Έ
    ‣ ॲཧ݁ՌΛHTML(ϒϥ΢β޲͚)΍JSON(ଞͷϓϩάϥϜ޲͚)Ͱ

    දݱ͢Δ࢓૊Έ
    ඞཁͳϞϊΛԾఆͯ͠४උϑϧελοΫ

    View full-size slide

  21. ‣ ҧ͍͸ʮ੍ޚͷํ޲ʯͱ͍͏͜ͱʹͳ͍ͬͯΔ
    ‣ ࣗ෼ͷॻ͍ͨίʔυͰݺͼग़͢ͷ͕ϥΠϒϥϦ
    ‣ ࣗ෼ͷॻ͍ͨίʔυΛݺͼग़ͯ͘͠ΕΔͷ͕ϑϨʔϜϫʔΫ
    ‣ Railsͩͱ: ϞσϧɺϏϡʔɺίϯτϩʔϥΛࣗ෼Ͱॻ͘ͱϢʔβͷ

    ϦΫΤετʹԠͯ͡ݺͼग़͞ΕΔ = MVCύλʔϯʹଇͬͨϑϨʔϜϫʔΫ
    ‣ (ͱ͸͍͑΍͍ͬͯΔ͏ͪʹϐϯͱདྷΔͱ͖͕͘ΔͷͰɺਂೖΓ͠ͳͯ͘Α͍)
    ༨ஊ
    ϥΠϒϥϦͱϑϨʔϜϫʔΫ

    View full-size slide

  22. ͓୊:
    ύʔιφϧϝϞΛ࡞Ζ͏

    View full-size slide

  23. ‣ ؆୯ͳRailsΞϓϦέʔγϣϯΛ࡞Δ
    ‣ ͦͷաఔͰWebΞϓϦʹඞཁͳॾ֓೦ʹ৮ΕΔ
    ‣ REST
    ‣ RDBMSઃܭ
    ͜ͷηογϣϯͰ΍Δ͜ͱ

    View full-size slide

  24. ‣ ඇಉظॲཧɺόονॲཧ
    ‣ JavaScript͘Θ͘͠
    ‣ CSS͘Θ͘͠
    ‣ RDBMSҎ֎ͷσʔλετΞ
    ‣ WebΞϓϦΛಈ͔͢Πϯϑϥ

    (Ϋϥ΢υɺεέʔϧΞ΢τɺίϯςφԽ)
    ͜ͷηογϣϯͰ΍Βͳ͍͜ͱ ཁࣗशPS͋ͱͰ࣭໰ͯ͠


    View full-size slide

  25. ϝϞΛೖྗͰ͖Δ

    View full-size slide

  26. ‣ https://ghe.example.com/moro/memo2
    ‣ Haml: HTML(XML)ͷग़ྗʹదͨ͠ςϯϓϨʔτݴޠ
    ‣ Bootstrap: Twitter͕ࣾఏڙ͍ͯ͠ΔCSSϑϨʔϜϫʔΫ
    SBJMTOFX͖ΐ͏࢖͏ϥΠϒϥϦΛ༻ҙ͓͖ͯ͠·ͨ͠

    View full-size slide

  27. ‣ αϯϓϧͷϦϙδτϦΛfork͠ɺࣗ෼ͷϦϙδτϦ͔Β

    clone͍ͯͩ͘͠͞ɻ
    ‣ RailsΞϓϦέʔγϣϯΛىಈͯ͠Έ·͠ΐ͏ɻ
    ‣ NewϦϯΫΛͨͲͬͯɺ͋ͨΒ͍͠ϝϞΛ1݅Ҏ্௥Ճ͠·
    ͠ΐ͏ɻ
    ࣮श0: 10෼

    View full-size slide

  28. $ git clone https://ghe.example.com//memo2.git
    $ cd memo2
    $ bundle install
    $ (brew services start postgresql)
    $ ./bin/rails db:create
    $ ./bin/rails db:migrate
    $ ./bin/rails s -p 3000
    $ open http://localhost:3000/memos
    3BJMTΞϓϦέʔγϣϯΛىಈ͠Α͏
    ‣ Ͳ͏ͳ͍ͬͯΔ͔ɺϒϥ΢βͰݟͯΈ·͠ΐ͏

    View full-size slide

  29. ͜ͷαϯϓϧͰ
    ΍ͬͯ͋Δ͜ͱ

    View full-size slide

  30. --- a/config/routes.rb
    +++ b/config/routes.rb
    @@ -1,3 +1,4 @@
    Rails.application.routes.draw do
    + resources :memos
    # For details on the DSL available within this file, see http://guides.rubyon
    routing.html
    end
    3BJMT͸3&45ͱ͍͏ߟ͑ํʹ΋ͱ͍͍ͮͯΔ
    ‣ 3&QSFTFOUBUJPOBM4UBUF5SBOTGFS
    ‣ )551ϝιου Ͳ͏͍ͨ͠
    ͱύεΫΤϦ ͳʹΛ
    Λͱ͍͏૊Έ߹ΘͤΛɺ

    Ϧιʔε΁ͷૢ࡞ɺͱͯ͠ந৅Խ͠ɺΞʔΩςΫνϟͷத৺ʹஔ͘ߟ͑ํ
    ‣ ͦͷϦιʔε΁ͷૢ࡞Λ΋ͬͯɺΞϓϦέʔγϣϯͷॲཧΛ࣮ݱ͢Δɻ
    ‣ ࠓճ͸ɺѻ͏ϝϞNFNPΛɺͦͷ··Ϧιʔεͱͨ͠ɻ

    View full-size slide

  31. $ ./bin/rails routes

    Prefix Verb URI Pattern Controller#Action
    memos GET /memos(.:format) memos#index
    POST /memos(.:format) memos#create
    new_memo GET /memos/new(.:format) memos#new
    edit_memo GET /memos/:id/edit(.:format) memos#edit
    memo GET /memos/:id(.:format) memos#show
    PATCH /memos/:id(.:format) memos#update
    PUT /memos/:id(.:format) memos#update
    DELETE /memos/:id(.:format) memos#destroy
    resources :memosͰఆٛ͞ΕΔૢ࡞
    ‣ ෳ਺औಘɺ୯਺औಘɺ࡞੒ɺߋ৽ɺ࡟আ
    ‣ ࡞੒ɺߋ৽ͷͨΊͷϑΥʔϜΛؚΉը໘΋Ϧιʔεͱߟ͑ΒΕΔ
    ‣ /memos͕ϝϞͷू߹Λද͠ɺͦͷू߹Λऔಘ (&5
    ͢Δ͜ͱΛ΋ͬͯɺ

    ʮϒϥ΢β্ʹϝϞΛҰཡදࣔʯͱ͍͏ΞϓϦέʔγϣϯͷॲཧΛදݱͨ͠ɻ

    View full-size slide

  32. ‣ POST /groups/42/add_member
    ‣ ϘσΟͰ person_id = 4Λࢦఆ͠ɺάϧʔϓͷ
    add_member ॲཧΛݺͼग़͢
    ‣ POST /groups/42/memberships
    ‣ ϘσΟͰ person_id = 4Λࢦఆ͠ɺ"άϧʔϓʹࢀՃ
    ͍ͯ͠Δ͜ͱ"(membership)Λ࡞੒͢Δ
    ༨ஊ
    Ϧιʔε͸Ϟϊ͚ͩͰͳ͍

    View full-size slide

  33. ༨ஊ
    Ϧιʔε͸Ϟϊ͚ͩͰͳ͍

    View full-size slide

  34. ‣ registration (ొ࿥͢Δ͜ͱ)ΛϦιʔεͱͯ͠

    ѻ͍ͬͯΔɻ
    ‣ ొ࿥ɾೝূج൫ͳͷͰɻϦιʔεͱͯ͠ѻ͏΂͖͜
    ͱͷཻ౓͸υϝΠϯ࣍ୈͰมΘΔɻ
    ‣ http://techlife.cookpad.com/entry/2017/04/06/172601
    ༨ஊ
    Ϧιʔε͸Ϟϊ͚ͩͰͳ͍

    View full-size slide

  35. +++ b/app/controllers/items_controller.rb
    @@ -0,0 +1,74 @@
    +class MemosController < ApplicationController
    + before_action :set_memo, only: [:show, :edit, :update, :destroy]
    +
    + # GET /memos
    + # GET /memos.json
    + def index
    + @memos = Memo.all
    + end
    +
    *UFNT$POUSPMMFSJOEFY
    ‣ ϧʔςΟϯά͔Βݺ͹ΕΔͱ͖͸ANFNPTJOEFYAදه
    ‣ .FNP͸3BJMTඪ४ͷ03.ɺ"DUJWF3FDPSE#BTFΛܧঝͨ͠Ϋϥεɻ
    ‣ .FNPBMMͰɺNFNPTςʔϒϧͷશϨίʔυΛಡΈࠐΈɺ

    !NFNPTʹೖΕΔ

    View full-size slide

  36. ‣ ϑϨʔϜϫʔΫͷಈ͖Λ͢΂ͯઃఆ͢Δ(configuration)͢ΔͷͰͳ͘ɺ

    Rails͕૝ఆ͢Δن໿(convention)ʹԊͬͯಈ͘ɻ
    ‣ ARΫϥεMemo͸ memos ςʔϒϧͷϨίʔυΛѻ͏
    ‣ GET /memos͸ MemosController#index ΛݺͼɺΞΫγϣϯॲཧޙ͸ app/
    views/memos/index.html.* ͷςϯϓϨʔτΛϨϯμϦϯά͢Δ
    ‣ ม਺໊ͰܕΛදݱ͢ΔจԽ΋͋Δɻ
    ‣ Railsʹ׳ΕΔͱmemos͸ෳ਺ͷMemoΠϯελϯεΛ͋ΒΘ͢ม਺໊ͱ

    ಡΈऔΔɻ
    $POWFOUJPOPWFS$POpHVSBUJPO

    View full-size slide

  37. ‣ ςϯϓϨʔτΛฤूͨ͠͏͑Ͱϒϥ΢βΛϦϩʔυ͠ɺද
    ͕ࣔมΘΔ͜ͱΛ֬ೝ͠·͠ΐ͏ɻ
    ‣ layout ͷԾஔ͖ͷαΠυόʔΛফͯ͠ΈΑ͏
    ‣ app/views/layouts/application.html.haml
    ‣ ૣ͘ऴΘͬͨΒ: τοϓϖʔδ(‘/‘)ͰϝϞͷҰཡΛදࣔͰ͖
    ΔΑ͏ʹ͠·͠ΐ͏ɻ
    ࣮श1: 5෼

    View full-size slide

  38. ‣ lesson-[n]-nantoka ͳͲͷϒϥϯνͰ։ൃ͢Δɻ
    ‣ ࣗϦϙδτϦ΁ͷmasterͱͯ͠ɺPRΛ࡞Δɻ
    ‣ title΍description΋Կ͔ॻ͘ɻ
    ‣ Ͳͷ՝୊޲͚͔ΛΘ͔ΔΑ͏ʹ͓͍͍ͯͯͩ͘͠͞ɻ
    ‣ ͋ͱͰಡΜͩਓ޲͚ͷจ຺΍޻෉ͨ͜͠ͱ(೚ҙ)ͳͲɻ
    ‣ ࣗϦϙδτϦ΁ʹϚʔδ͢Δɻ
    ࣮शఏग़ํ๏

    View full-size slide

  39. ‣ RailsΛ࢖͍͸͡Ίͨ
    ‣ ͍ΖΜͳgem͕͋Δ
    ‣ Convention over Configuration
    ‣ RESTfulͳΞʔΩςΫνϟʹ;Εͨ
    ͜ͷεςοϓͰֶΜͩ͜ͱ

    View full-size slide

  40. $ vim config/routes.rb
    Rails.application.routes.draw do
    root to: 'memos#index'
    resources :memos
    end
    ࢓্͛τοϓϖʔδͰϝϞͷҰཡΛදࣔ͠Α͏

    View full-size slide

  41. Ϣʔβ͝ͱʹ

    ϝϞ͕औΕΔ

    View full-size slide

  42. ߟ͑ΔʮϢʔβ͝ͱϝϞʯͷσʔλߏ଄

    ユーザ タイトル 内容
    Alice きょうやること
    - インターンに参加する
    - 懇親する
    Alice 買い出し
    ## 薬局
    - 風邪薬
    ## スーパー
    - 牛乳2本
    - 卵があれば6つ
    Bob 明日の持ち物 印鑑

    View full-size slide

  43. ߟ͑ΔʮϢʔβʯͷॏෳΛݮΒ͢ /' /'


    タイトル 内容
    きょうやること
    - インターンに参加する
    - 懇親する
    買い出し
    ## 薬局
    - 風邪薬
    ## スーパー
    - 牛乳2本
    - 卵があれば6つ
    明日の持ち物 印鑑
    ユーザ
    Alice
    Bob

    View full-size slide

  44. 3BJMT෺ཧઃܭ1, ',Λಋೖ͢Δ

    id user_id タイトル 内容
    1 1 きょうやること
    - インターンに参加する
    - 懇親する
    2 1 買い出し
    ## 薬局
    - 風邪薬
    ## スーパー
    - 牛乳2本
    - 卵があれば6つ
    3 2 明日の持ち物 印鑑
    id ユーザ
    1 Alice
    2 Bob

    View full-size slide

  45. ‣ ݱΫοΫύουͷActiveRecordϞσϧΫϥε(ҎԼARϞσϧ)User͸ϑΝΠϧத
    Ͱఆٛ͞ΕΔϝιου͕328ݸɺinclude͍ͯ͠ΔϞδϡʔϧ͕14ݸɺ175ݸͷؔ
    ࿈͕ఆٛ͞Ε͍ͯΔ3,000ߦΛ௒͑ΔΫϥεͰ͢ɻ
    ‣ υϝΠϯʹ͓͚ΔҙຯʹΑͬͯɺςʔϒϧ͸෼ׂ͍͖ͯ͠·͠ΐ͏ɻ
    ‣ Α͋͘Δྫ: users ςʔϒϧʹ email / password ͸͋Δ΂͖͔
    ‣ Ұൠతʹ͸ɺগͳ͘ͱ΋࿦ཧઃܭͰ͸ࡉ͔͘෼͚͍ͯ͘΄͏͕ϕλʔɻ
    ༨ஊ
    Ϣʔβʔςʔϒϧʹ͋Δ΋ͷɺͦ͏Ͱͳ͍΋ͷ

    View full-size slide

  46. $ ./bin/rails g model user name:string
    $ ./bin/rails g migration AddUserRefToMemos user:references
    $ ./bin/rails db:migrate
    VTFSTςʔϒϧͱɺNFNPTVTFS@JEΛ௥Ճ͢Δ

    View full-size slide

  47. $ cat db/schema.rb
    create_table "memos", force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "user_id"
    t.index ["user_id"], name: "index_memos_on_user_id", using: :btree
    end
    create_table "users", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    end
    add_foreign_key "memos", "users"
    VTFSTςʔϒϧͱɺNFNPTVTFS@JEΛ௥Ճ͢Δ

    View full-size slide

  48. $ ./bin/rails console
    > User.create!(name: 'alice')
    > User.create!(name: 'bob')
    > exit
    ϢʔβͷμϛʔσʔλΛ࡞Δ

    View full-size slide

  49. ‣ ϢʔβΦϒδΣΫτ͔Βॴ༗͢ΔϝϞΛऔಘͰ͖ͨΓɺ

    ϝϞ͔ΒͦͷϢʔβΛऔಘͰ͖ͨΓ͢Δͱศར
    ‣ ActiveRecord Ͱ͸ςʔϒϧؒ(ARΫϥεؒ)ͷؔ࿈Λ

    දݱͰ͖Δ
    ‣ ARͰͷݴ͍ճ͠͸ Association
    ߟ͑ΔϢʔβ͝ͱʹϝϞ͕͋Δɺͱ͸

    View full-size slide

  50. ‣ ʮϝϞ͸1ਓͷϢʔβʹଐ͢Δʯ
    ‣ Memo belongs_to :user
    ‣ memos͕΋͍ͬͯΔuser_idʹΑͬͯɺଐ͢Δ1ਓͷϢʔβ
    Λࢀর͢Δ
    ‣ ϢʔβΛࢀর͢ΔͨΊͷ#userͱ͍͏ϝιουͳͲ͕࢖͑
    ΔΑ͏ʹͳΔ
    ଟରPSରͷʮଐ͢Δʯؔ࿈ੑ

    View full-size slide

  51. ‣ ʮϢʔβ͸ɺෳ਺ͷϝϞΛॴ༗͍ͯ͠Δʯ
    ‣ User has_many :memos
    ‣ memos ͕΋͍ͬͯΔuser_id ʹΑͬͯɺॴ༗͢Δnݸ
    ͷϝϞΛࢀর͢Δ
    ‣ ෳ਺঎඼Λࢀর͢ΔͨΊͷ#memosͱ͍͏ϝιου
    ͳͲ͕࢖͑ΔΑ͏ʹͳΔ
    ରଟͷʮॴ༗͢Δʯؔ࿈ੑ

    View full-size slide

  52. $ vim app/models/user.rb
    class User < ActiveRecord::Base
    has_many :memos
    end
    $ vim app/models/memo.rb
    class Memo < ActiveRecord::Base
    belongs_to :user
    end
    ؔ࿈Λઃఆ͢Δ

    View full-size slide

  53. $ ./bin/rails console
    > user = User.find_by!(name: ‘alice')
    > user.name
    > user.memos
    > user.memos = Memo.all
    > memo = Memo.first
    > memo.user
    طଘͷϝϞ΋ಛఆͷϢʔβʹؔ࿈෇͚Δ

    View full-size slide

  54. ‣ ͜͜·Ͱͷઆ໌Λ΋ͱʹɺUserΫϥεΛ࡞੒͠ɺ

    ؔ࿈ͳͲΛఆٛ͠ͳ͍͞ɻ
    ‣ લͷϖʔδͷσʔλϚΠάϨʔγϣϯΛ͠Α͏ɻ
    ‣ ૣ͘ऴΘͬͨΒ:
    ‣ Rails guideͷʮActive Record ͷؔ࿈෇͚ʯΛ

    ಡΜͰΈΑ͏
    ࣮श2: 15෼

    View full-size slide

  55. Ϣʔβͱͯ͠ϩάΠϯͰ͖Δ

    View full-size slide

  56. ‣ ϩάΠϯૢ࡞
    ‣ ϢʔβຊਓͷΈ͕஌͍ͬͯΔ৘ใͰೝূ͠ɺϩάΠϯঢ়ଶ
    ʹ͢Δૢ࡞
    ‣ ϩάΠϯঢ়ଶ
    ‣ ͦͷαʔϏεΛ࢖͍ͬͯΔϢʔβΛɺ8FCΞϓϦ

    ͔ΒҰҙʹࣝผͰ͖Δঢ়ଶ
    ‣ ຊਓͷΈʹڐ͞Εͨૢ࡞΍Ϧιʔε΁ͷΞΫηε͕Ͱ͖Δ
    Α͏ʹͳΔ
    ϩάΠϯͱ͸Կ͔

    View full-size slide

  57. ‣ ʮϩάΠϯૢ࡞ʯ= ೝূͷ࣮૷͸΍Γ·ͤΜɻ

    ͍ΖΜͳ΋ͷ͕͋Δɻ
    ‣ Ϣʔβ໊ + ύεϫʔυ
    ‣ SNS (OAuth2 ΍ OpenID)
    ‣ ϫϯλΠϜύεϫʔυ+ϩάΠϯϦϯΫ
    ‣ SSLΫϥΠΞϯτೝূ
    ‣ ෺ཧσόΠε etc…
    ʲ஫ҙʳ͖ΐ͏΍Βͳ͍͜ͱ

    View full-size slide

  58. ‣ WebΞϓϦέʔγϣϯͰ͸ʮηογϣϯʯͱ͍͏

    ߟ͑ํͰ࣮ݱ͞ΕΔ͜ͱ͕ଟ͍ɻ
    ϩάΠϯঢ়ଶ

    View full-size slide

  59. ‣ େલఏ: HTTP͸ঢ়ଶΛ࣋ͨͳ͍ϓϩτίϧͰ͋Δɻ
    ‣ αʔόଆʹɺΫϥΠΞϯτঢ়ଶΛอ࣋Ͱ͖ͳ͍ɻ
    ‣ ෳ਺ฒΜͩαʔόͷͲΕʹϦΫΤετ͕ߦ͔ܾ͘ఆͰ͖ͳ͍ɻ
    ‣ ࣮༻తͳαʔϏεΛ࡞ΔͨΊʹ͸ɺٖࣅతʹʮঢ়ଶʯΛ

    ࣋ͨͤΔඞཁ͕͋Δɻ
    ‣ Ұ౓ϩάΠϯͨ͠Β͠͹Βͦ͘ͷਓͱͯ͠ೝূ͞Ε͍ͯΔɺͱ͔ɻ
    ‣ ͜ͷʮ͋Δ࣌ؒ୯ҐͰಉҰϢʔβ͕ܧଓతʹαʔϏεΛ࢖ͬ
    ͍ͯΔঢ়ଶʯΛηογϣϯͱݺͿɻ
    ηογϣϯͱ͸

    View full-size slide

  60. ‣ ΫϥΠΞϯτ(ϒϥ΢β)͕ৗʹૹग़͢ΔσʔλʹࣝผࢠΛ
    ೖΕɺαʔόଆͰΫϥΠΞϯτঢ়ଶΛอଘ͢Δ͜ͱͰ

    ʮηογϣϯʯΛදݱ͍ͯ͠Δɻ
    ‣ CookieΛར༻͢Δ͜ͱ͕ଟ͍ɻ
    ‣ WebΞϓϦϑϨʔϜϫʔΫ͕ػೳΛఏڙͯ͘͠ΕΔɻ
    CookieʹΑΔʮηογϣϯʯͷ࣮ݱ

    View full-size slide

  61. ‣ WebΞϓϦʹ͓͍ͯɺαʔό͕ΫϥΠΞϯτʹ

    ʮ࣍ʹૹͬͯ΄͍͠σʔλʯΛ౉ͤΔɻ
    ‣ Set-Cookie: ϔομ
    ‣ ΫϥΠΞϯτ͸࣍ͷϦΫΤετ࣌ʹ Cookie ϔομʹͦͷσʔλΛೖΕͯૹΔ
    ‣ ※ ཁ͸ΫϥΠΞϯτ͕೚ҙͷσʔλΛૹ৴Ͱ͖Δɻ
    Cookie

    View full-size slide

  62. ‣ ίϯτϩʔϥͷ #session ͰΞΫηεͰ͖Δɻ
    ‣ ෳ਺ϦΫΤετΛލ͍ͰσʔλΛอ࣋Ͱ͖Δɻ
    ‣ ͋·Γͨ͘͞ΜͷσʔλΛೖΕͳ͍ͷ͕Rails way
    ‣ ϩάΠϯϢʔβͷid΍ɺ஫จͷidͳͲ
    ‣ ϦΫΤετΛड͚ͨ͋ͱidͰDB͔ΒҾ͖௚ͯ͠࢖͏
    RailsͰͷηογϣϯ࣮૷

    View full-size slide



  63. (1) Cookie: (なし) (2) uid=42なセッション開始
    (3) {uid: 42} をキー abcdeで保存
    (4) Set-Cookie: sid=abcde
    (5) Cookie: sid=abcde (6) キーabcdeでセッション
    データを検索
    (7) {uid: 42} として処理
    Ұൠతͳηογϣϯͷ࣮ݱ

    View full-size slide


  64. (1) Cookie: (なし)
    (2) uid=42なセッション開始
    (3) Set-Cookie: {uid: 42}
    (4) Cookie: {uid: 42}
    (6) 署名を検証
    (7) {uid: 42} として処理
    RailsͷCookieετΞηογϣϯ

    View full-size slide

  65. ‣ લड़ͷΑ͏ʹɺCookie͸UA(ϒϥ΢β)͕ೖΕͯ͘Δ
    ʮϢʔβʔ͔Βͷೖྗ஋ʯͰ͋Δɻ
    ‣ Rails ͷηογϣϯ͸ɺηογϣϯΩʔΛ༧ଌෆೳͳ
    ஋ʹͨ͠Γɺվ͟Μ๷ࢭͷ࢓૊ΈΛೖΕ͍ͯΔ
    (CookieετΞ)ɻ
    ‣ ଞͷϔομ΋ಉ༷ɻ
    (༨ஊ) ϔομ΍Cookie ΋վ͟ΜͰ͖Δ

    View full-size slide

  66. ‣ αʔϏεҰൠʹ͓͍ͯʮҰ࿈ͷϢʔβʔߦಈʯΛࢦ͢

    (֓೦)ηογϣϯɻ
    ‣ ֓೦ʙͷߦಈͷؒͰͰσʔλΛ࣋ͪճͨ͢ΊʹWebͰ͸
    CookieͳͲͰ࣮ݱ͞ΕΔ(ػೳ)ηογϣϯɻ
    ‣ ػೳʙΛ࣮ݱ͢ΔͨΊʹɺRailsఏڙ͢Δ`#session`ϝιου
    ͳͲ(Rails࣮૷)ηογϣϯɻ
    ͍ΖΜͳηογϣϯؔ܎͋Δ͚ͲҐ૬͕ҧ͏ͷͰ஫ҙ

    View full-size slide

  67. $ vim config/routes.rb

    # REST的に考えると「ログインする」のは(概念)セッションを作ること
    # 1ブラウザにつき1個なので単数リソース
    Rails.application.routes.draw do
    root to: 'memos#index'
    resources :memos
    resource :session
    end
    ࣮૷ͯ͠ΈΑ͏

    View full-size slide

  68. $ vim app/views/sessions/new.html.haml
    = form_tag session_path, class: "form-horizontal" do
    .form-group
    .col-lg-offset-2.col-lg-4
    %label ログインユーザ
    - opts = options_for_select(User.all.map {|u| [u.name, u.id] })
    = select_tag :user_id, opts, class: 'form-control'
    .form-group
    .col-lg-offset-2.col-lg-10
    = submit_tag 'ログインする', class: 'btn btn-primary'
    = link_to 'キャンセル', root_path, class: 'btn btn-default'
    $ vim app/controllers/sessions_controller.rb
    class SessionsController < ApplicationController
    def new
    end
    end
    $ open http://localhost:3000/session/new
    ϩάΠϯϑΥʔϜ͸TFTTJPOTOFXʹ͢Δ

    View full-size slide

  69. $ tail -f log/development.log
    ϩάΛݟͳ͕Β࡞ۀ͠·͠ΐ͏

    View full-size slide

  70. ‣ (؆қ)ೝূ͢ΔΞΫγϣϯɺ
    `SessionsController#create`Λ࣮૷͠Α͏
    ‣ ౉͞Ε͖ͯͨ user_id Ͱfindͯ͠sessionʹೖΕΔ
    ‣ ૣ͘ऴΘͬͨΒ:
    ‣ φϏήʔγϣϯόʔʹ͜ͷϩάΠϯը໘΁ͷϦϯΫΛ͚ͭΑ͏ɻ
    ‣ RailsΨΠυͷηΩϡϦςΟ߲ΛಡΈɺηογϣϯݻఆ߈ܸʹֶ͍ͭͯ΅͏
    ‣ ϩάΞ΢τͰ͖ΔΑ͏ʹ͠Α͏
    ࣮श3: 20෼

    View full-size slide

  71. class SessionsController < ApplicationController
    def new
    end
    def create
    user = User.find_by(id: params[:user_id])
    if user
    reset_session
    session[:user_id] = user.id
    redirect_to root_path, notice: 'Login successed'
    else
    redirect_to root_path, alert: 'Login failed'
    end
    end
    end
    ճ౴ྫ

    View full-size slide

  72. # —- リンク
    = link_to "ログアウト", session_path, method: :delete
    ͪͳΈʹϩάΞ΢τ

    View full-size slide

  73. ‣ ϩάΠϯதͷϢʔβΛը໘ʹදࣔ͠Α͏
    ‣ session[:user_id]ͰҾ͖ɺίϯτϩʔϥͷΠϯελϯεม਺ʹೖΕΔ or ΞΫηαϝιουΛ࡞Ζ͏
    ‣ ׳ྫͩͱ current_user ͱ͍͏໊લ
    ‣ Railsίϯτϩʔϥͷ helper_method એݴΛௐ΂ͯΈΑ͏
    ‣ શίϯτϩʔϥͰ࢖͏ͨΊʹ ApplicaitionController ʹఆٛ͠Α͏
    ‣ ը໘ͷͲ͔͜Ͱදࣔ͠Α͏
    ‣ ૣ͘ऴΘͬͨΒ:
    ‣ ϩάΠϯঢ়ଶΛ൑ఆ͢ΔϝιουΛ࡞Ζ͏
    ‣ φϏήʔγϣϯόʔͷϩάΠϯϦϯΫΛɺϩάΠϯத͸ϩάΞ΢τϦϯΫʹ͠Α͏
    ࣮श4: 30෼

    View full-size slide

  74. class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception
    helper_method :logged_in?, :current_user
    private
    def logged_in?
    current_user.present?
    end
    def current_user
    if session[:user_id]
    @current_user ||= User.find(session[:user_id])
    end
    end
    end
    ճ౴ྫϝιουԽͯ͠

    View full-size slide

  75. %ul.nav.navbar-nav.navbar-right
    - if logged_in?
    %li
    %p.navbar-text
    Hello
    %strong= current_user.name
    %li= link_to "ログアウト", session_path, method: :delete
    - else
    %li= link_to "ログイン", new_session_path
    .container
    ճ౴ྫϏϡʔͰදࣔͰ͖ΔΑ͏ʹͨ͠

    View full-size slide

  76. ‣ ActiveRecordͰ User has_many memos ؔ࿈͕͋Δͱ
    ‣ user.memos ͰϢʔβʹؔ࿈͍ͮͨϝϞΛҾ͚Δ
    ‣ user.memos.create! ͰϢʔβʹؔ࿈͍ͮͨϝϞΛ

    ࡞ΕΔ
    Ϣʔβ͝ͱʹϝϞΛ࡞Ζ͏

    View full-size slide

  77. ‣ ࡞੒͢ΔϝϞΛɺϩάΠϯதͷϢʔβʹؔ࿈෇͚·
    ͠ΐ͏ɻ
    ‣ ૣ͘ऴΘͬͨΒ:
    ‣ ϝϞҰཡͰදࣔͰ͖ΔϝϞΛɺϩάΠϯதϢʔβ͚ͩͷ΋ͷʹ͠·͠ΐ͏ɻ
    ‣ ฤूɺ࡟আͰ͖ΔͷΛࣗ෼ͷϝϞ͚ͩʹ͠·͠ΐ͏
    ࣮श5: 30෼

    View full-size slide

  78. ϝϞΛڞ༗Ͱ͖Δ

    View full-size slide

  79. ‣ ϝϞ࡞ऀͱͯ͠ɺڞ༗Ͱ͖Δɻ
    ‣ ڞ༗͞Εͨଆͱͯ͠ɺϝϞͷ಺༰ΛදࣔͰ͖Δɻ
    ‣ ฤूɾ࡟আ͸Ͱ͖ͳ͍
    ߟ͑Δʮڞ༗ʯͷ࢓༷Λߟ͑Δ

    View full-size slide

  80. ‣ ʮڞ༗͢Δʯखஈͱͯ͠ʮൿີͷURLʯΛ࡞੒͢Δɻ
    ‣ ϝϞ࡞ऀ͸ɺಛఆͷϝϞΛڞ༗ऀʹݟͤΔͨΊͷ

    ʮൿີͷURLʯΛൃߦͰ͖Δɻ
    ‣ ڞ༗ଆ͸ɺͦͷURLʹΞΫηε͢ΔͱϝϞͷ಺༰ΛදࣔͰ
    ͖Δɻ
    ࢓༷͔Β࣮૷Λߟ͑Δ

    View full-size slide

  81. ‣ ʮڞ༗͞ΕͨϝϞʯ͸Ͳ͏͍͏Ϧιʔεͳͷ͔ɻ
    ‣ Ͳ͏ଊ͔͑ͨɺPRʹॻ͍͓͍͍ͯͯͩ͘͞ɻ
    ‣ ൿີͷURLʹظݶ͸͋Δ͔?
    ‣ ෳ਺ͷൿີͷURL͸ൃߦͰ͖Δ΂͖͔?
    ‣ ߋ৽ʹ௥ਵ͢΂͖͔?
    ࣮૷Λߟ͑Δ ଓ͖


    View full-size slide

  82. ‣ (ERਤ) secret_memos belongs_to memos
    ‣ uuid
    ‣ expires_at
    ‣ …
    ‣ ଟॏ౓Ͳ͏͢ΔɺফͤΔ?…
    %#ઃܭ

    View full-size slide


  83. memos
    title:string
    body:text
    rails timestamps
    secret_links
    memo_id:integer
    uuid:string
    …?
    rails timestamp
    *
    1

    View full-size slide

  84. ‣ ࡞੒ͨ͠ϝϞΛڞ༗Ͱ͖ΔΑ͏ʹ͠·͠ΐ͏ɻ
    ‣ ϝϞ࡞ऀ͕ൿີͷURLΛൃߦͰ͖Δ
    ‣ ڞ༗͞Εͨଆ͕ϝϞͷ಺༰ΛݟΒΕΔ
    ‣ ͦͷଞʮϝϞͷڞ༗ʯͱͯ͠ద੾ͩΖ͏࢓༷Λ

    ߟ࣮͑૷͠ͳ͍͞
    ࣮श6: ऴΘΔ·Ͱ

    View full-size slide

  85. ‣ RailsΛ࢖ͬͯ؆୯ͳWebΞϓϦέʔγϣϯΛ࡞Δɻ
    ‣ ͦΕΛ௨ͯ͡ɺWebΞϓϦέʔγϣϯʹඞཁͳཁૉ
    ٕज़ʹ;ΕΔɻ
    ‣ REST: αʔϏεͷཁૉΛʮϦιʔεͷಡΈॻ͖ʯͱଊ͑Δ
    ‣ DBઃܭ: ਖ਼نԽͨ͠σʔλΛARܦ༝Ͱ͞ΘΔ
    ‣ ηογϣϯ: HTTPͰΫϥΠΞϯτঢ়ଶΛѻ͏ఆ൪Λ஌Δ
    ࠓ೔ͷΰʔϧ

    View full-size slide

  86. ‣ Git
    ‣ ϦϏδϣϯΛ؅ཧ͢Δ࢓૊Έɻcommit ͑͋͞Ε͹
    ͳΜͱ͔ͳΔɻίϥϘϨʔγϣϯؤுΖ͏ɻ
    ‣ TDD
    ‣ ςετͰ։ൃΛʮۦಈʯ͢Δɻ։ൃɾઃܭख๏ɻ
    ‣ WebͱRails
    ‣ ৭ʑਂಡΈ͍ͯ͘͠ͱ͓΋͠Ζ͍Ͱ͠ΐɻ
    ࠓ೔ͷΰʔϧ ऴ೔


    View full-size slide

  87. ͓ർΕ͞·Ͱͨ͠

    View full-size slide