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

Rails Controller Fundamentals

Rails Controller Fundamentals

D0ea189be896384bd6f0cef9956f32de?s=128

SAKAGUCHI Takashi

September 24, 2018
Tweet

Other Decks in Technology

Transcript

  1. Rails Controller Fundamentals — ͱͬͱΔͼʔୈ31ճ 2018-09-24 — https://tottoruby.connpass.com/event/100646/ — @hamajyotan

    (SAKAGUCHI Takashi)
  2. ͖͞ʹ͓఻͑ — ͪΐͬͱͨ͠ Rails ίʔυΛಈ͔ͨ͠Γ͠·͢. ҎԼ ͷঢ়ଶ͕खݩʹ͋Δͱ, Ұॹʹࢼͨ͠ΓͰ͖·͢. — Ruby

    2.5.1 , Rails 5.2.1 Ͱ࣮ࢪ͠·͕͢, ͦ͜·Ͱ ݫີʹ߹ΘͤΔඞཁ͸ͳ͍Ͱ͢. — gem sqlite3. (rails new Ͱ database Λࢦఆ͠ͳ͍ ͷͰطఆͰ࢖͍ͬͯΔ͚ͩͰ͋Γ, ߦؒಡΜͰผ DB ΛࢦఆͰ͖Ε͹ͦΕͰ OK Ͱ͢)
  3. ࣗݾ঺հ — SAKAGUCHI Takashi — Office UMMM LLC — Ruby

    ͱ͔ Rails ͱ͔͕ͪΐͬͱͰ͖Δ͓͡͞ΜͰ͢
  4. ࠓ೔࿩͢͜ͱ — scaffold Α͘ΈͯΈΑ͏ͱ͍͏݅ — resource routing ʹͩ͜ΘΖ͏ͱ͍͏݅ — template

    inheritance ศརͩΑͱ͍͏݅
  5. scaffold Α͘ΈͯΈΑ͏ͱ͍͏݅

  6. ؆୯ͳΞϓϦΛ࡞Γͭͭ $ ruby -v ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

    $ rails -v Rails 5.2.1 $ rails new example --skip-active-storage ग़ྗলུ... $ cd example/ $ ./bin/rails s ➡ http://localhost:3000
  7. ؆୯ͳΞϓϦΛ࡞Γͭͭ $ ./bin/rails scaffold message \ title body:text \ published:boolean

    $ ./bin/rails db:migrate ➡ http://localhost:3000/ messages
  8. resource(s) ͳϧʔςΟϯά config/routes.rb Rails.application.routes.draw do resources :messages end $ rails

    routes Prefix Verb URI Pattern Controller#Action messages GET /messages(.:format) messages#index POST /messages(.:format) messages#create new_message GET /messages/new(.:format) messages#new edit_message GET /messages/:id/edit(.:format) messages#edit message GET /messages/:id(.:format) messages#show PATCH /messages/:id(.:format) messages#update PUT /messages/:id(.:format) messages#update DELETE /messages/:id(.:format) messages#destroy
  9. ΞΫγϣϯͱͦͷҙຯ Action ҙຯ index Ұཡදࣔ(ը໘) create ৽ن࡞੒ new ৽ن࡞੒(ը໘) edit

    ฤू(ը໘) show ৄࡉදࣔ(ը໘) update ߋ৽ destroy ࡟আ
  10. html ౎߹ͷ new/edit ͸͜ͷ͍͞ஔ͍ͱ͘ Resource HTTP Method Action ҙຯ /messages

    GET index Ұཡදࣔ(ը໘) /messages POST create ৽ن࡞੒ /message/:id GET show ৄࡉදࣔ(ը໘) /message/:id PATCH/PUT update ߋ৽ /message/:id DELETE destroy ࡟আ
  11. HTTP ͷϝιου HTTP Method /messages /message/:id GET index show PUT/PATCH

    - update DELETE - destroy POST create -
  12. HTTP ͷϝιου HTTP Method ҙਤͳͲ ҆શ ႈ౳ GET Ϧιʔεͷऔಘ !

    ! PUT/PATCH Ϧιʔεͷ഑ஔ !❓ DELETE Ϧιʔεͷ࡟আ !❓ POST Ϧιʔεͷੜ੒
  13. GET — Ϧιʔεͷऔಘ. ৗʹ҆શͰႈ౳. — scaffold ͷ࣮૷ͩͱ, ίϨΫγϣϯ΁ͷ GET ͸ৗʹ

    200 Ͱϝϯόʔ΁ͷ GET ͸ͳ͍৔߹ 404 Λฦ͢. def index @messages = Message.all end def show end
  14. PUT/PATCH — Ϧιʔεͷ഑ஔ. — scaffold ͷ࣮૷ͷ৔߹ଘࡏ͠ͳ͍৔߹͸ 404 Λฦ͢ ͕, ఆ͔ٛΒ͢Ε͹

    ID Λ໌ࣔͨ͠ merge (insert or update) ͱͨ͠ํ͕ద౰͔΋. def update respond_to do |format| if @message.update(message_params) format.html { redirect_to @message, notice: 'Message was successfully updated.' } format.json { render :show, status: :ok, location: @message } else format.html { render :edit } format.json { render json: @message.errors, status: :unprocessable_entity } end end end
  15. DELETE — Ϧιʔεͷ࡟আ. — scaffold ͷ࣮૷ͷ৔߹ଘࡏ͠ͳ͍৔߹͸ 404 Λฦ͢ ͕, ఆ͔ٛΒ͢Ε͹ଘࡏ͠ͳ͍৔߹͸Կ΋͠ͳ͍ͷ͕

    ద౰͔΋. def destroy @message.destroy respond_to do |format| format.html { redirect_to messages_url, notice: 'Message was successfully destroyed.' } format.json { head :no_content } end end
  16. POST — Ϧιʔεͷੜ੒ — ʮੜ੒ʯͱ͍ͯ͠Δͷ͸ίϨΫγϣϯϦιʔεʹର ͢Δૢ࡞Ͱ͋ΔͨΊ. def create @message =

    Message.new(message_params) respond_to do |format| if @message.save format.html { redirect_to @message, notice: 'Message was successfully created.' } format.json { render :show, status: :created, location: @message } else format.html { render :new } format.json { render json: @message.errors, status: :unprocessable_entity } end end end
  17. 60ߦ͢͜͠ class MessagesController < ApplicationController before_action :set_message, only: [:show, :edit,

    :update, :destroy] def index @messages = Message.all end def show; end def new @message = Message.new end def edit; end def create @message = Message.new(message_params) respond_to do |format| if @message.save format.html { redirect_to @message, notice: 'Message was successfully created.' } format.json { render :show, status: :created, location: @message } else format.html { render :new } format.json { render json: @message.errors, status: :unprocessable_entity } end end end def update respond_to do |format| if @message.update(message_params) format.html { redirect_to @message, notice: 'Message was successfully updated.' } format.json { render :show, status: :ok, location: @message } else format.html { render :edit } format.json { render json: @message.errors, status: :unprocessable_entity } end end end def destroy @message.destroy respond_to do |format| format.html { redirect_to messages_url, notice: 'Message was successfully destroyed.' } format.json { head :no_content } end end private def set_message @message = Message.find(params[:id]) end def message_params params.require(:message).permit(:title, :body, :published) end end
  18. resource routing ʹͩ͜ΘΖ͏ͱ͍͏݅

  19. ϝοηʔδʹ͓ؾʹೖΓΛ͚͍ͭͨ — ͬͯཁ݅Λߟ͑ͯΈΔ. — ಛఆͷϝοηʔδʹର͓ͯ͠ؾʹೖΓʹͨ͠ΓͦΕ Λ֎ͨ͠ΓͰ͖Δ — ࠓճ͸ʮ୭͕ʯ͓ؾʹೖΓΛ͚ͭΔͷ͔? ͱ͍͏৘ใ ͸αϘΔ

    — ຊ࣭Ͱͳ͍ͷͰ, ؆୯ʹ favorited:boolean ͷ ϑϥάΛ Message Ϟσϧʹ͚ͭΔ͚ͩʹͯ͠Δ.
  20. favorited ϑϥάΛ༻ҙ͢Δ $ ./bin/rails g migration AddFavoritedToMessages favorited:boolean $ ./bin/rails

    db:migrate
  21. ͲͷΑ͏ͳΞΫγϣϯΛ४උ͢΂͖͔? resources :messages do post :add_favorite, on: :member post :remove_favorite,

    on: :member end $ ./bin/rails routes Prefix Verb URI Pattern Controller#Action add_favorite_message POST /messages/:id/add_favorite(.:format) messages#add_favorite remove_favorite_message POST /messages/:id/remove_favorite(.:format) messages#remove_favorite ... class MessagesController < ApplicationController def add_favorite end def remove_favorite end ... ➡ (´ɾТɾ`)…
  22. ͲͷΑ͏ͳΞΫγϣϯΛ४උ͢΂͖͔? resources :messages do put :favorite, on: :member delete :favorite,

    on: :member end $ ./bin/rails routes Prefix Verb URI Pattern Controller#Action favorite_message PUT /messages/:id/favorite(.:format) messages#favorite DELETE /messages/:id/favorite(.:format) messages#favorite ... class MessagesController < ApplicationController def favorite # favorite & unfavorite ? end ... ➡ (´ɾТɾ`)……
  23. ͲͷΑ͏ͳΞΫγϣϯΛ४උ͢΂͖͔? resources :messages do put :favorite, on: :member, action: 'add_favorite'

    delete :favorite, on: :member, action: 'remove_favorite' end $ ./bin/rails routes Prefix Verb URI Pattern Controller#Action favorite_message PUT /messages/:id/favorite(.:format) messages#add_favorite DELETE /messages/:id/favorite(.:format) messages#remove_favorite ... class MessagesController < ApplicationController def add_favorite end def remote_favorite end ... ➡ (´ɾТɾ`)………
  24. ϧʔςΟϯάͷઃܭΨΠυϥΠϯ — resource(s) Ͱఏڙ͞ΕΔΞΫγϣϯҎ֎Λආ͚Δ — index, show, new, create, edit,

    update, destroy ͷΈ — config/routes.rb Ͱ get ͱ͔ post ͱ͔Λͳ Δ΂͘࢖Θͳ͍.
  25. ϧʔςΟϯάͷઃܭΨΠυϥΠϯ — ࣮ݱ͢Δ͜ͱ͔Β໊ࢺΛ୳͠, ੵۃతʹαϒϦιʔε ͱͯ͠σβΠϯ͢Δ — /messages/:message_id/favorite

  26. ϧʔςΟϯάͷઃܭΨΠυϥΠϯ — ࠓճͷྫͰ͸ /messages/:message_id/ favorite ʹରͯ͠ PUT/DELETE Ͱ࣮ݱ — DELETE

    ͔ͩΒͱݴͬͯ, ࣮ࡍʹ DB ্ͰσʔλΛ ࡟আ͢Δඞཁ͸ͳ͍. — URI ͷϦιʔεʹର͠, DB ͰͷӬଓԽํ๏͸શ ͘ผͷ࿩. — DBߏ੒ม͑Δͨͼʹ URIߏ੒ม͑ͳ͍Ͱ͠ΐ?
  27. ࠶ߟ - ϧʔςΟϯά resources :messages do resource :favorite, only: %i[update

    destroy], module: 'messages' end $ ./bin/rails routes Prefix Verb URI Pattern Controller#Action message_favorite PATCH /messages/:message_id/favorite(.:format) messages/favorites#update PUT /messages/:message_id/favorite(.:format) messages/favorites#update DELETE /messages/:message_id/favorite(.:format) messages/favorites#destroy ...
  28. ࠶ߟ - ίϯτϩʔϥ # app/controllers/messages/favorites_controller.rb class < Messages::FavoritesController before_action :set_message

    def update respond_to do |format| if @message.update(favorited: true) format.html { redirect_to @message, notice: 'Favorited.' } else format.html { redirect_to @message, alert: 'Favorite failed.' } end end end def destroy respond_to do |format| if @message.update(favorited: false) format.html { redirect_to @message, notice: 'Unfavorited.' } else format.html { redirect_to @message, alert: 'Unfavorite failed.' } end end end private def set_message @message = Message.find(params[:message_id]) end end # (json লུ.)
  29. template inheritance ศརͩΑͱ͍͏݅

  30. template inheritance #ͱ͸ — Ϗϡʔ͸ͲͷσΟϨΫτϦ/ϑΝΠϧΛ୳ࡧ͢Δ? — app/views/ίϯτϩʔϥ໊/ΞΫγϣϯ໊.֦ுࢠ Έ͍ͨͳͱ͜Ζ? — ➡

    ͍͍ͩͨ͋ͬͯΔ͚Ͳ΋͏ͻͱ͍͖ — ➡ ❓
  31. template inheritance #ͱ͸ — Ϗϡʔ͸ͲͷσΟϨΫτϦ/ϑΝΠϧΛ୳ࡧ͢Δ? — app/views/ίϯτϩʔϥ໊/ΞΫγϣϯ໊.֦ுࢠ Έ͍ͨͳͱ͜Ζ? — ➡

    ͍͍ͩͨ͋ͬͯΔ͚Ͳ΋͏ͻͱ͍͖ — ➡ ίϯτϩʔϥͷܧঝπϦʔ͕ώϯτʹͳ͍ͬͯΔ
  32. view template ͷ୳ࡧॱং — ίϯτϩʔϥͷܧঝπϦʔ — MessagesController — < ApplicationController

    — ➡ ҎԼͷॱʹ୳ࡧ͞ΕΔ 1. app/views/messages/* 2. app/views/application/*
  33. view template ͷ୳ࡧॱং — ྫ͑͹ Messages ͷαϒΫϥεͷ Foos Λ࡞ͬͨΒ —

    FoosController < MessagesController — ➡ ҎԼͷॱʹ୳ࡧ͞ΕΔ 1. app/views/foos/* 2. app/views/messages/* 3. app/views/application/*
  34. ͪΐͬͱͨΊͦ͏ $ mkdir app/views/application $ mv app/views/messages/index.html.erb app/views/application/ ➡ ৔ॴҠͯ͠΋ಈ͘.

    $ cp app/views/application/index.html.erb app/views/messages/index.html.erb $ echo '<h2>΄͛</h2>' >> app/views/messages/index.html.erb ➡ messages/ ͕༏ઌ.
  35. ΈΜͳ ApplicationController Λܧঝͯ͠Δͱ͍͏͜ͱ͸ — app/views/application/ ͸ڞ௨ϏϡʔςϯϓϨ ʔτஔ͖৔ʹ࢖͑Δ. — ͔͠΋, ৔߹ʹΑͬͯ͸֤ʑͷίϯτϩʔϥͰ্ॻ͖

    Ͱ͖Δ. — ͜Ε஌Βͣʹ app/views/shared/* ͱ͔࡞ΔΑΓ Rails way ͔ͱ.
  36. ͨͱ͑͹͜ΜͳϧʔςΟϯά — /messages - ϝοηʔδҰཡ — ͜Εʹରͯ͠ɺ /messages/draft ͱ͍͏ URL

    Λ४ උ͢Δ — جຊతʹ͸ /messages ͱಉ͡. — ͨͩ͠, published ͕ false ͷσʔλͰϑΟϧλ ͞Εͨঢ়ଶͱ͢Δ.
  37. /messages/draft Λͭ͘Δ - 1/4 app/controllers/messages_controller.rb def index - @messages =

    Message.all + @messages = message_scope end private + def message_scope + Message.all + end + def set_message
  38. /messages/draft Λͭ͘Δ - 2/4 app/controllers/messages/ drafts_controller.rb class Messages::DraftsController < MessagesController

    private def message_scope Message.where(published: false) end end
  39. /messages/draft Λͭ͘Δ - 3/4 config/routes.rb Rails.application.routes.draw do + namespace :messages

    do + resources :drafts, only: %i[index] + end resources :messages end
  40. /messages/draft Λͭ͘Δ - 4/4 config/routes.rb Rails.application.routes.draw do namespace :messages do

    - resources :drafts, only: %i[index] + resources :drafts, only: %i[index], path: 'draft' end resources :messages end ➡ Ҏ্ ❗ Ϗϡʔͷमਖ਼ͳͲ͸ͳ͠
  41. ·ͱΊ — scaffold ͷు͘ίϯτϩʔϥ͸ྑ͍ίʔυ. — routes ʹ͸ͳΔ΂͘ resource(s) ͚ͩΛهड़. —

    URIͰσβΠϯ͢ΔϦιʔεͱ DBӬଓԽ͸ผͷ࿩. — template inheritance Λ͏·͘࢖͓͏. — ڞ௨ͷςϯϓϨʔτ͸ app/views/application/ σΟϨΫτϦ