Share
Embed
Transcript
103 slides
-
Կނ͔ 1. ࠓ͚ͩͰ৭ʑͳ͜ͱΛڭ͑·͢ɻ 2. Ұؾʹ֮͑ΔͷແཧͰࣗ৴Λࣦ͏͔͠Ε·ͤΜɻ 3. ͔͠໌͔Β Rails ࿔Γ·ͤΜɻ 4.
ͱ͍͏Θ͚ͰɺRails Ε͔͚ͯΔͱࢥͬͨΒ Rails νϡʔ τϦΞϧ1 Λࢥ͍ग़͢ͱྑ͍Ͱ͠ΐ͏ɻ 1 http://railstutorial.jp/ -
2. ௨৴Λ։࢝Ͱ͖ΔͷΫϥΠΞ ϯτ͔ΒͷΈͰ͢ 1. ͳͷͰ push ௨ͳͲग़དྷ·ͤΜ 2. push ௨ͬΆ͍ΛΔͨΊʹ৭ʑେมʢServer
Sent Events ͱ͔ௐΔͱ৭ʑग़ͯ͘Δʣ -
HTTP(Hypertext Transfer Protocol) ৮ͬͯΈ·͠ΐ͏ $ telnet cookpad.com 80 Trying 54.65.132.171...
Connected to cookpad.com. Escape character is '^]'. ͦͷ··ԼهͷΑ͏ʹଧͬͯΈ·͢ɻ GET / HTTP/1.1 Host: cookpad.com ͦͷޙɺReturn key Λೋճԡ͠·͢ɻ ͩΒʙ ͬͱΫοΫύουͷσʔλ͕औಘग़དྷ·ͨ͠ɻ -
HTTP(Hypertext Transfer Protocol) 4 1ߦʢϦΫΤετߦʣ 4 GETʢHTTP ϝιουʣ 4 /
(Path) 4 HTTP/1.1 ʢϓϩτίϧ໊ʣ 4 ೋߦҎ߱ʢϝοηʔδϔομʣ 4 Host: cookpad.comʢϗετϔ ομʣ -
HTTP(Hypertext Transfer Protocol) Path ෦Λม͑ͯΈ·͠ΐ͏ GET /search HTTP/1.1 Host: cookpad.com
ऴྃ Ctrl + ] Λ͓ͨ͠ޙʹ quit ͱίϚϯυΛଧͯग़དྷ· ͢ɻ -
HTTP(Hypertext Transfer Protocol) HTTP ϝιου෦Λม͑ͯΈ·͠ΐ͏ HEAD /search HTTP/1.1 Host: cookpad.com
ಉ͡ Path Ͱ GET ͷ࣌ͱҧ͏ͷ͕ฦ͖ͬͯ·ͨ͠ɻ -
REST ࢥʹ͍ͭͯ ͬ͘͟Γઆ໌ 4 ૢ࡞ͷରͱͳΔϦιʔεΛ URL Ͱදݱ͢Δ 4 ͦΕʹ HTTP
ͷϝιουΛͬͯૢ࡞͢Δ -
REST ࢥʹ͍ͭͯ ྫ͑ /items/5 ͱ͍͏ URL ͕Ұͭͷ item Λද͍ͯͯ͠ 4
HTTP ϝιουͷ GET ͰΞΫηε͢Δͱͦͷ item ͕औಘ Ͱ͖Δ 4 HTTP ϝιουͷ PATCH ͰΞΫηε͢Δͱͦͷ item ͷ ใΛߋ৽Ͱ͖Δ 4 HTTP ϝιουͷ DELETE ͰΞΫηε͢Δͱͦͷ item Λ আͰ͖Δ -
REST ࢥʹ͍ͭͯ ಉ͡Α͏ʹ /items ͱ͍͏ URL item ͷू߹Λද͍ͯ͠ΔͷͰ 4
HTTP ϝιουͷ GET ͰΞΫηε͢Δͱ item ͷू߹ʢҰ ཡʣ͕औಘͰ͖Δ 4 ྫ͑ /items/1, /items/2, /items/3, /items/4 …… 4 HTTP ϝιουͷ POST ͰΞΫηε͢Δͱ item ͷू߹ʹ৽͠ ͍ item ΛՃ͢Δ ͱ͍͏;͏ʹͳΓ·͢ -
Rails ͷ MVC ੌ͘୯७ʹॲཧΛॻ͘ͱ…… 4 controllerʢͱ routesʣ Ϣʔβʔ͔ΒͷೖྗΛݩʹ model Λऔಘ͠ɺview
ʹ͠·͢ 4 view ͞Εͨ model Λදࣔ͠·͢ ͱ͍͏ಈ͖Λ͠·͢ɻ୯७Ͱ͢Ͷɻ -
ͬͦ͘͞ Rails ΞϓϦΛ࡞Γ·͠ΐ ͏ ࠓճ࡞ΔΞϓϦը૾ͷ URL Λߘͯ͠ΈΜͳͰͦͷը૾ʹίϝϯτ͕ ग़དྷΔΞϓϦέʔγϣϯʹͳΓ·͢ɻ ͖͞΄Ͳͷ REST
తʹݴ͏ͱϦιʔε 4 ը૾ͷ URL 4 ը૾ͷ URL ʹର͢Δίϝϯτ ͷ 2 ͭʹͳΓ·͢ɻ -
ͬͦ͘͞࡞Γ͡Ί·͠ΐ͏ $ rails new bokete -T Ͱ Rails ΞϓϦͷͻͳܗ͕ग़དྷ·͢ɻ $
cd bokete ͯͦ͠ͷσΟϨΫτϦʹೖΓ·͢ɻ ࠓޙ͜ͷσΟϨΫτϦΛΞϓϦέʔγϣϯͷ HOME ͱͯ͠ ѻ͍جຊతʹ͜͜ͰίϚϯυΛ࣮ߦ͠·͢ɻ -
Bundler ৭ʑͳϥΠϒϥϦΛूதཧͯ͘͠ΕΔͷͰ͢ɻ ·ͣ Rspec ͱ haml ΛՃ͠·͢ɻ Gemfile ʹԼهΛه͠·͢ gem
"haml-rails", "~> 0.9" gem 'rspec-rails', '~> 3.0', group: [:development, :test] -
haml Խ Gemfile هࡌޙɺԼهΛ࣮ߦ͠·͢ $ bundle install ͜ΕͰϥΠϒϥϦͷΠϯετʔϧ͕ऴΘΓ·ͨ͠ɻ ࣍ʹςϯϓϨʔτΛ haml
ʹ౷Ұ͠·͢ (ݩʑ ERB ͩͬͨͷΛ haml ʹίϯόʔτ͠·͢) $ rails generate haml:application_layout convert $ rm app/views/layouts/application.html.erb -
RSpec ४උ $ rails generate rspec:install Ͱ rspec ͷॳظԽΛߦ͍·͢ $
bundle exec rspec Ͱ GREEN Λ֬ೝͨ͠Β commit ͓͖ͯ͠·͠ΐ͏ $ git init $ git add . $ git commit -m "init" -
image Ϟσϧ࡞ ը૾ URL ͱ λΠτϧΛอ࣋͢ΔϞσϧΛ࡞͠·͢ɻ $ rails generate model
image url:string title:string invoke active_record create db/migrate/xxxxxxxxxxx_create_images.rb create app/models/image.rb invoke rspec create spec/models/image_spec.rb DB ͷߏΛมߋ͢Δ migration ϑΝΠϧͱϓϩάϥϛϯά͔Β DB ͷσʔλΛૢ࡞͢ΔϞσϧ͕࡞͞Ε·ͨ͠ɻ -
image Ϟσϧ࡞ DB ఆٛ ઌͣ DB ͷߏΛมߋ͢Δ migration ϑΝΠϧΛ࿔Γ·͢ db/migrate/xxxxxxxxxxxcreateimages.rb
Λ։͖ url ͱ title ΧϥϜΛ NOT_NULL ʹ͠·͢ class CreateImages < ActiveRecord::Migration def change create_table :images do |t| t.string :url, null: false # Ճ t.string :title, null: false # Ճ t.timestamps null: false end end end -
image Ϟσϧ࡞ DB ఆٛ ࣮ࡍʹσʔλϕʔεʹ migration ϑΝΠϧΛө͠·͢ɻ ࠓճςετ༻ͷ DB ʹөͤ͞·͢
$ bin/rake db:migrate RAILS_ENV=test rspec ࣮ߦͯ͠Έ·͠ΐ͏ $ bundle exec rspec pending ʹͳΓ·͢Ͷʢmodel Կ࣮͍ͯ͠ͳ͍ͷͰʣ -
image Ϟσϧ࡞ όϦσʔγϣϯ ͬͦ͘͞ model Λ࣮͍͖ͯ͠·͠ΐ͏ɻDB ͷ੍ΛϰΝϦσʔγϣϯ͠·͢ɻ require 'rails_helper' RSpec.describe
Image, type: :model do describe "validation" do describe "url" do let(:no_url_image) { Image.new(url: nil, title: "title") } it "require url" do expect(no_url_image).to_not be_valid expect(no_url_image.errors[:url]).to_not be_empty end end end end rspec Λ࣮ߦ͢Δͱ͘ͳΓ·͢Ͷ -
image Ϟσϧ࡞ όϦσʔγϣϯ valid Ͱͳ͍͜ͱΛظͨ͠ͷʹ valid ͩͬͨͱݴΘΕ·͢Ͷɻ validation Λ model
ʹՃ͠ valid Ͱແ͘͠·͠ΐ͏ɻ class Image < ActiveRecord::Base validates :url, presence: true end ಉ͡Α͏ʹ title ͷ ϰΝϦσʔγϣϯ࣮͠·͠ΐ͏ɻ rspec Λ࣮ߦͯ͢͠௨Δ͜ͱΛ֬ೝͯ͠ίϛοτ͠·͢ɻ $ git add . $ git commit -m "Add image model" -
Image ίϯτϩʔϥʔ࡞ Spec ༻ҙ͠·͢ spec/controllers/images_controller_spec.rb require 'rails_helper' RSpec.describe ImagesController, type:
:controller do describe "#index" do it "assigns @images" do get :index expect(assigns(:images)).to_not be_nil end end end ςετͷ༰ͷઆ໌ޙ΄Ͳ͠·͢ͷͰͱΓ࣮͋͑ͣߦͯ͠Έ·͠ΐ͏ -
routes ͱ resources Failure/Error: get :index ActionController::UrlGenerationError: No route matches
{:action=>"index", :controller=>"images"} images ͱ͍͏ controller ͷ index ͍ͬͯ͏ action ʹϚον ͢Δ route ͕ແ͍ΑͱݴΘΕ·͢ɻ -
routes ͱ resources config/routes.rb ͱ͍͏ϑΝΠϧʹԼهΛ͔͍ͯΈ·͢ Rails.application.routes.draw do resources :images end
ͪΐͬͱલʹઆ໌ͨ͠ʮREST ࢥʹ͍ͭͯʯΛࢥ͍ग़͍ͯͩ͘͠͞ 4 ૢ࡞ͷରͱͳΔϦιʔεΛ URL Ͱදݱ͢Δ 4 ͦΕʹ HTTP ͷϝιουΛͬͯૢ࡞͢Δ routes ʹ images ͱ͍͏ϦιʔεΛૢ࡞͢Δͱઃఆͨ͠Θ͚Ͱ͢ɻ -
routes ͱ resources $ rake routes Prefix Verb URI Pattern
Controller#Action images GET /images(.:format) images#index POST /images(.:format) images#create new_image GET /images/new(.:format) images#new edit_image GET /images/:id/edit(.:format) images#edit image GET /images/:id(.:format) images#show PATCH /images/:id(.:format) images#update PUT /images/:id(.:format) images#update DELETE /images/:id(.:format) images#destroy Ͳͷ HTTP ϝιουͰͲͷ URL ʹɺΞΫηε͢ΔͱɺͲͷ Controller ͷɺͲͷ Action ͕ݺΕΔͷ͔ग़͖ͯ·ͨ͠ɻ -
routes ͱ resources spec Ͱ֬ೝ͍ͯ͠Δͷ GET ϝιουͰ index ʹΞΫηε͢ΔͷͰͨ͠ɻ ͜Ε
URI Pattern ͱͯ͠ /images ʹͳΓ·͢ɻ ͜ΕઌఔͷʮREST ࢥʹ͍ͭͯʯͰݴ͏ͱ 4 HTTP ϝιουͷ GET ͰΞΫηε͢Δͱ item ͷू߹ʢҰཡʣ͕औಘͰ͖ Δ 4 ྫ͑ /items/1, /items/2, /items/3, /items/4 …… ʹͳΓ·͢ɻ Ͱɺroute Λઃఆͨ͠ͷͰ rspec Λ࣮ߦͯ͠Έ·͠ΐ͏ɻ -
Image ίϯτϩʔϥʔ࡞ Failure/Error: get :index AbstractController::ActionNotFound: The action 'index' could
not be found for ImagesController ImagesController ʹ index ΞΫγϣϯ͕ແ͍ͱݴΘΕ·ͨ͠ɻ Ճ͠·͠ΐ͏ class ImagesController < ApplicationController def index end end rspec Λ࣮ߦͯ͠Έ·͠ΐ͏ɻ -
Image ίϯτϩʔϥʔ࡞ Failure/Error: get :index ActionView::MissingTemplate: Missing template images/index, application/index
with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in: * "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007fdcbde03d90>" ࠓ Template ͕ແ͍ͱݴΘΕ·ͨ͠ app/views/images/index.html.haml ͱ͍͏ϑΝΠϧΛ࡞Γ ·͢ rspec Λ࣮ߦͯ͠Έ·͠ΐ͏ɻ -
Image ίϯτϩʔϥʔ࡞ Failure/Error: expect(assigns(:images)).to_not be_nil expected: not nil got: nil
ࠓ ͳʹΒ not nil Λظ͍ͯ͠Δͷʹ nil ͩͱݴΘΕ·ͨ͠ɻ ͯ͞ɺઌ΄Ͳͷ spec Ͱ͕͢ get :index expect(assigns(:images)).to_not be_nil GET ϝιουͰ index ʹΞΫηεͨ͠Β imagesʢimage ͷू߹ʣ͕ΞαΠ ϯ͞Ε͍ͯΔ͜ͱΛ֬ೝ͍ͯ͠·ͨ͠ɻ -
Image ίϯτϩʔϥʔ࡞ Ͱ࣮ࡍʹΞαΠϯͯ͠Έ·͠ΐ͏ class ImagesController < ApplicationController def index @images
= Image.all end end rspec Λ࣮ߦͯ͠Έ·͠ΐ͏ɻͯ͢௨Γ·͢Ͷɻ -
index view ࡞ ςετ͕ͯ͢௨ͬͨͷͰ࣮ࡍʹ֬ೝͯ͠Έ·͢ɻ ·ͣ։ൃڥͷ DB ΛηοτΞοϓ͠ىಈͯ͠Έ·͢ɻ $ bin/rake db:migrate
RAILS_ENV=development $ rails server http://localhost:3000/images/ ʹΞΫηεͯ͠Έ·͠ΐ͏ɻ -
index view ࡞ ·ͩ view ΛԿ࡞͍ͬͯͳ͍ͷͰදࣔ͞Εͳ͍ͷͨΓલͰ͢Ͷɻ ·ͣ app/views/images/index.html.haml ʹ %h1
ը૾Ұཡ ͱɺॻ͍ͯϦϩʔυͯ͠Έ·͠ΐ͏ɻ -
index view ࡞ haml ʹ͍ͭͯ ͬ͘͟Γ 4 html tag ߏΛ
YAML ͬΆ͘දͨ͠ςϯϓϨʔτΤϯδϯ 4 % Ͱී௨ʹλά -> %ul 4 - Ͱ ruby ͷߏจ -> - @images.each do |image| 4 = Ͱ ग़ྗ -> = image.title -
index view ࡞ Image ͕Ұͭσʔλϕʔεʹొ͍ͯ͠ͳ͍ͷͰͦͦҰཡʹग़͢͜ͱ͕ग़དྷ·ͤΜɻ ొͯ͠Έ·͠ΐ͏ɻ $ rails console Loading
development environment (Rails 4.2.2) irb(main):001:0> Image.create!(url: "http://i.giphy.com/I6l7Kwu0GLvAA.gif", title: "starwars") index.html.haml ʹදࣔ෦ΛՃ͠·͢ %h1 ը૾Ұཡ %dl - @images.each do |image| %dt= image.title %dd= image_tag image.url Ϧϩʔυ͢Δͱදࣔ͞Ε·͢ -
controller ͱ view ͞Βͬͱ͓͞Β͍ routes ແ͍Α -> action ແ͍Α ->
ςϯϓϨʔτແ͍Α -> มΞ αΠϯ͞Εͯͳ͍Α spec Λॻ͍͓ͯ͘͜ͱʹΑΓɺ࣮ߦΛ͢ΔͨͼʹΔ͖͜ͱΛ ڭ͑ͯΒ͍ͳ͕ΒҰาͮͭਐΊ·ͨ͠ɻ ͦͯ͠Τϥʔ͕ແ͘ͳͬͨΒ view Λ࣮͠·ͨ͠ɻ ࠓճ view ςετΛॻ͔ͣʹͰ֬ೝ͠ͳ͕Β࣮͠·ͨ͠ɻ ͜͜ͰҰ୴ίϛοτ͓͖ͯ͠·͠ΐ͏ɻ -
image ొը໘ جૅઆ໌ͨ͠ͷͰ͘͘͞͞ߦ͖·͢ spec/controllers/images_controller_spec.rb ʹొը໘ͷ spec ΛՃ͠·͢ɻ describe "#new" do
it "assigns @image" do get :new expect(assigns(:image)).to_not be_nil end end spec ௨͢ͱ new ΞΫγϣϯ͕ແ͍ͱݴΘΕ·͢ -
image ొը໘ app/views/images/new.html.haml spec ͕௨ΔΑ͏ʹͳΔͷͰ࣮ࡍͷ view Λॻ͖·͢ = form_for @image
do |f| = f.label :title = f.text_field :title = f.label :url = f.text_field :url = f.submit http://localhost:3000/images/new Λ։͍ͯΈ·͢ -
form_for ʹ͍ͭͯ = form_for @image do |f| form_for Ҿʹ͞Ε͍ͯΔΦϒδΣΫτʹΑͬͯݺͼग़͠ઌΛม͑ͯ͘Ε·͢ɻ ྫ͑
@image ͕ DB ʹطʹอଘ͞Ε͍ͯͨΒ PUT /images/:id(.:format) images#update ͕ݺΕɺ·ͩอଘ͞Ε͍ͯͳ͍߹ POST /images(.:format) images#create ͕ݺΕ·͢ɻ -
form_for ʹ͍ͭͯ ͭ·Γ Controller Ͱ @image = Image.new ͱ͖ͯͬ͠͞ͷ view
Λදࣔͨ࣌͠৽نొ @image = Image.find(params[:id]) ͱ͖ͬ͞ͷ view Λදࣔͨ࣌͠طଘσʔλͷฤूʹͳΔΑ͏ ʹࣗಈతʹݺͼग़͠ઌΛม͑ͯ͘Ε·͢ -
image ৽نొ ࣮ࡍʹσʔλΛొ͢Δ෦Λ࡞͠·͠ΐ͏ spec/controllers/images_controller_spec.rb ʹ৽نొͷ spec ΛՃ͠·͢ɻ describe "#create" do
it "create new image" do expect { post :create, image: { url: "http://example.com/", title: "test" } }.to change { Image.count }.from(0).to(1) end end ࠓճ࣮ࡍʹొ͞Εͯݸ͕ 0 -> 1 ʹͳ͍ͬͯΔ͜ͱΛ֬ೝ͍ͯ͠·͢ spec ௨͢ͱ create ΞΫγϣϯ͕ແ͍ͱݴΘΕ·͢ -
image ৽نొ ొྃը໘Λग़͢ͷͰྑ͍Ͱ͕͢ࠓճొ͕ޭͨ͠Β ҰཡϖʔδʹભҠ͢ΔΑ͏ʹ͠·͠ΐ͏ def create Image.create(params.require(:image).permit(:url, :title)) redirect_to action:
:index end http://localhost:3000/images/new Λ։͍ͯొͰ͖Δ͜ ͱΛ֬ೝͰ͖ͨΒίϛοτ͠·͠ΐ͏ -
ొը໘ͷϦϯΫ app/views/images/index.html.haml ʹԼهΛՃ͠·͢ɻ = link_to "ը૾Λߘ͢Δ", new_image_path ઌ΄Ͳ࣮ߦͨ͠ rake routes
Ͱग़͖ͯͨ prefix ʹ _path Λ ͚ͭΔͱͦ͜ͷ path ʹͳΓ·͢ɻ ͳͷͰࠓճ new_image_path ʹͳΓ·͢ɻ ·ͨίϛοτ͠·͢ɻ -
ը૾ݸผϖʔδ spec/controllers/images_controller_spec.rb describe "#show" do let(:image) { Image.create(url: "http://example.com/", title:
"test") } it "assigns @image" do get :show, id: image.id expect(assigns(:image)).to eq image end end ͬ͘͞Γ rspec ࣮ߦͯ͠ action ແ͍ͱݴΘΕ·͢ɻ -
ը૾ݸผϖʔδ app/views/images/show.html.haml %h1= @image.title = image_tag @image.url app/views/images/index.html.haml ͪΐͬͱม͑·͢ %dd
= link_to image do = image_tag image.url ಈ࡞֬ೝ͕Ͱ͖ͨΒίϛοτ͠·͢ɻ -
ը૾আ spec/controllers/images_controller_spec.rb describe "destroy" do let!(:image) { Image.create(url: "http://example.com/", title:
"test") } it "destroy image" do expect { delete :destroy, id: image.id }.to change { Image.count }.from(1).to(0) end end ͬ͘͞Γ rspec ࣮ߦͯ͠ action ແ͍ͱݴΘΕ…… -
ը૾আ def destroy Image.find(params[:id]).destroy redirect_to action: :index end আޙ index
ߦ͘Α͏ʹ͠·͢ spec ͕௨Δ͜ͱΛ֬ೝ͠·͢ɻ -
ը૾আ app/views/images/show.html.haml ʹআ༻ͷϦϯΫΛ Ճ͠·͢ %h1= @image.title = image_tag @image.url =
link_to "আ", @image, method: :delete ಈ࡞֬ೝ͠ίϛοτ͠·͢ɻ -
github push 1. New repository ΛΫϦοΫ͠৽͍͠ repository Λ࡞Γ·͢ 2.
repository name ࣗͷ໊લ -rails-app ʹ͍ͯͩ͘͠͞ɻ 1. ԶͳΒ yoshiori-shoji-rails-app ޙखॱ௨Γ git remote add origin git@xxxx:yoshiori/yoshiori-shoji-rails-app.git git push -u origin master Ͱ͢ɻʢϦϩʔυͯ͠Έ·͠ΐ͏ʣ -
github ͔Β clone ࣍ͷ։ൃ PR ΛૹͬͯΈ·͢ 1. ྡͷਓͱϖΞΛΜͰ͍ͩ͘͞ 2. ͦͷਓͷΞϓϦΛ
git clone ͠·͢ 3. bundle install & bin/rake db:migrate ͠·͢ ͊͞ɺࠓ͔ΒίνϥΛ࿔Γ·͢ɻ -
branch ࣍ը૾ʹίϝϯτΛ͚ΔػೳΛ࡞͍͖ͬͯ·͢ɻ ৽͍͠ػೳͰ͢ͷͰϒϥϯνΛ࡞ͬͯ࡞ۀ͠·͠ΐ͏ɻ $ git checkout -b add_images_comment $ git
branch * add_images_comment master ͕ࣗͲͷϒϥϯνʹډΔ͔֬ೝ͓͖ͯ͠·͠ΐ͏ ʢ͍͍ͩͨͷਓ͕λʔϛφϧʹΤσΟλʹग़ΔΑ͏ʹ͠·͢ʣ -
ίϝϯτ model ͷ࡞ ίϝϯτϞσϧΛ࡞͠·͢ɻ ࠓճը૾ʹԿݸίϝϯτΛ͚ΒΕΔΑ͏ʹ͢ΔͷͰҰରଟͷؔ࿈ͱͯ͠ද͠·͢ $ rails generate model comment
image:references body:text class CreateComments < ActiveRecord::Migration def change create_table :comments do |t| t.references :image, index: true, foreign_key: true, null: false t.text :body, null: false t.timestamps null: false end end end body ΧϥϜΛ NOT_NULL ʹ͠·͢ -
ίϝϯτ model ͷ࡞ ͬ͘͞ͱόϦσʔγϣϯΛ࣮͍͖ͯ͠·͠ΐ͏ require 'rails_helper' RSpec.describe Comment, type: :model
do describe "validation" do let(:image) { Image.create(url: "http://example.com/", title: "test") } describe "body" do subject(:no_body_comment) { image.comments.build(body: nil) } it "require" do expect(no_body_comment).to_not be_valid expect(no_body_comment.errors[:body].size).to be 1 end end end end -
ίϝϯτ model ͷ࡞ DB ʹมߋ͕͋ͬͨͷͰ $ bin/rake db:migrate RAILS_ENV=test Λ࣮ߦ͔ͯ͠Β
spec Λ࣮ߦ͢Δͱམͪ·͢ɻ Failure/Error: subject(:no_body_comment) { image.comments.build(body: nil) } NoMethodError: undefined method `comments' for #<Image:0x007fd9c9a2b020> # ./spec/models/comment_spec.rb:8:in `block (4 levels) in <t ͦͦ image.comments.build ͱ͍͏ॻ͖ํԿͳͷͰ͠ΐ͏͔ -
ϦϨʔγϣϯγοϓ ϞσϧͦΕͧΕʹؔ࿈ใΛ࣋ͨͤ·͢ class Image < ActiveRecord::Base validates :url, presence: true
validates :title, presence: true + has_many :comments end class Comment < ActiveRecord::Base belongs_to :image end ͜ΕͰ Image ͱ Comment ΛҰରଟͱͯؔ͠࿈͚ΒΕ·ͨ͠ɻ -
ϦϨʔγϣϯγοϓ ͜ΕͰ image.comments.build ͱ࣮ߦ͢Δͱ image ʹؔ࿈ͮ ͍ͨ Comment Λ৽͘͠࡞͢Δ͜ͱ͕ग़དྷ·͢ɻ ࣮ࡍԼهॲཧ΄΅ಉͩ͡ͱࢥͬͯΒͬͯେৎͰ͢
> image.comments.build(body: "test") > Comment.new(image_id: image.id, body: "test") -
ίϝϯτ model ͷ࡞ ͯ͞ɺspec ʹ͖ͬͯ·͢ class Comment < ActiveRecord::Base validates
:body, presence: true belongs_to :image end -
ίϝϯτ model ͷ࡞ ؔ࿈ςετ͓͖ͯ͠·͠ΐ͏ describe "image_id" do context "empty" do
subject(:no_image_id_comment) { Comment.new(image_id: nil, body: "body") } it "invalid" do expect(no_image_id_comment).to_not be_valid expect(no_image_id_comment.errors[:image].size).to be 1 end end context "illegal id" do subject(:illegal_image_id_comment) { Comment.new(image_id: image.id + 1, body: "body") } it "invalid" do expect(illegal_image_id_comment).to_not be_valid expect(illegal_image_id_comment.errors[:image].size).to be 1 end end end -
ίϝϯτ model ͷ࡞ class Comment < ActiveRecord::Base validates :body, presence:
true validates :image, presence: true belongs_to :image end spec ͕௨Δ͜ͱΛ֬ೝͯ͠ίϛοτ͠·͢ɻ -
ίϝϯτߘ spec/controllers/comments_controller_spec.rb require 'rails_helper' RSpec.describe CommentsController, type: :controller do let(:image)
{ Image.create(url: "http://example.com/", title: "text") } describe "#create" do it "create new comment" do expect { post :create, image_id: image.id, comment: { body: "ίϝϯτ" } }.to change { Comment.count }.from(0).to(1) end end end -
ؔ࿈ resources ͷઃఆ ͜ΕΛ routes Ͱද͢ͱ resources :images do resources
:comments end ͱද͠·͢ɻ rake routes ͯ͠ URI ͕Ճ͞Ε͍ͯΔ͜ͱΛ֬ೝͯ͠Έ· ͠ΐ͏ -
ίϝϯτߘ ࣮ࡍͷ create ΞΫγϣϯΛ࣮͠·͢ class CommentsController < ApplicationController def create
image = Image.find(params[:image_id]) image.comments.create(params.require(:comment).permit(:body)) redirect_to image_path(image) end end image Λऔಘͯͦ͠Εʹؔ࿈͢ΔίϝϯτΛ৽͘͠࡞͠·͢ɻ ྃͨ͠Β image ͷݸผϖʔδʹભҠ͢ΔΑ͏ʹ͠·ͨ͠ɻ spec ͕௨Δ͜ͱΛ֬ೝ͠·͠ΐ͏ -
ίϝϯτߘ ίϝϯτߘը૾ϖʔδ͔Β͍ͨ͠ͷͰը૾ϖʔδʹ form ΛՃ͠·͢ app/views/images/show.html.haml %h2 ίϝϯτ %ul - @image.comments.each
do |comment| %li = comment.body = form_for [@image, Comment.new] do |f| = f.text_field :body = f.submit form_for ͷ͠ํ͕ͪΐͬͱบ͕͋ΔͷͰҙͰ͢ -
Pull request Ͱ࣮ͨ͠ίϝϯτػೳΛϓϧϦΫΤετͱͯ͠ग़ͯ͠Έ·͢ɻ 1. ͖ͬ͞ͷ clone ͨ͠ϦϙδτϦͷϖʔδͰ fork ϘλϯΛԡ͢ 2.
fork ͨ͠ url Ͱ git remote add mine #{url} ͢Δ 3. git push mine add_images_comment 4. fork ͨ͠ϖʔδΛ։͖ϓϧϦΫΤετϘλϯΛԡ͢ ऴΘͬͨΒٯʹࣗͷϦϙδτϦ͖ͨ PR ΛϚʔδ͠·͠ΐ͏ ࣗͷϓϩδΣΫτͷσΟϨΫτϦʹΓ git pull origin master ͠ɺઌ΄ͲϚʔδͨ͠ͷ͕ϩʔΧϧʹө͞ΕͨͷΛ֬ೝ ͠·͠ΐ͏ɻ -
API Խ ࠓ͔Β json API Λ࡞͍͖ͬͯ·͠ΐ͏ɻ ·ͣ؆୯ʹ JSON ͷ spec
Λॻ͚Δ rspec-json_matcher Λಋೖ͠·͢ɻ Gemfile ʹ gem "rspec-json_matcher", group: :test Λ͠ bundle install ͠·͢ -
API Խ rake routes ͨ࣌͠ͷදࣔ images GET /images(.:format) images#index ͜͜ͷ
URI Pattern ͷඞͣޙΖʹ͍͍ͯΔ (.:format) ͕ΩϞͰ͢ɻ Ͳ͏͍͏͜ͱ͔ͱ͍͏ͱ http://localhost:3000/images ʹΞΫηε͢Δͱී ௨ʹ html ͕ฦ͖ͬͯ·͕͢ɺhttp://localhost:3000/images.json ͱ֦ு ࢠ෩ʹ format Λࢦఆͯ͠ΞΫηε͢Δͱ json ͕ฦͬͯ͘ΔΑ͏ʹग़དྷ·͢3ɻ 3 format ͷࢦఆͷํଞʹ͋Γ·͢ -
API Խ Ͱͬͦ͘͞ spec Ͱ͢ɻࠓճ json ͕ฦͬͯ͘Δ͜ͱ͕େࣄͳͷͰ request spec Λॻ͖·͢ɻ
spec/requests/images_spec.rb Λ࡞Γ·͢ require "rails_helper" RSpec.describe Image, type: :request do let!(:image) { Image.create(url: "http://example.com/", title: "test") } describe "GET /images.json" do it "return json" do get "/images.json" expect(response.body).to be_json( images: [ id: image.id, title: "test", url: "http://example.com/", ], ) end end end -
API Խ spec Λ࣮ߦͯ͠ΈΔͱ·ͨςϯϓϨʔτ͕ແ͍ͱݴΘΕ·͕͢Α͘ݟΔͱ formats json ʹͳ͍ͬͯ·͢ɻࠓճ rails ඪ४ͷ
jbuilder Ͱॻ͍ͯ Έ·͢ app/views/images/index.json.jbuilder Λ࡞Γ·͢ json.images @images, :id, :title, :url spec Λ࣮ߦ͠௨Δ͜ͱΛ֬ೝͨ͠ΒϒϥβͰ http://localhost: 3000/images.json Λ։͍ͯ֬ೝͯ͠Έ·͠ΐ͏ɻ ͳ͚Είϛοτ͠·͢ɻ -
API Խ ͜ͷ·· show ࣮ͯ͠Έ·͠ΐ͏ describe "GET /images/:id.json" do let!(:comment)
{ image.comments.create!(body: "test") } it "return json" do get "/images/#{image.id}.json" expect(response.body).to be_json( id: image.id, title: "test", url: "http://example.com/", comments: [ { id: comment.id, body: "test", }, ], ) end end -
API Խ app/views/images/show.json.jbuilder json.(@image, :id, :title, :url) json.comments @image.comments, :id,
:body spec Λ࣮ߦ͠௨Δ͜ͱΛ֬ೝͨ͠ΒϒϥβͰ http:// localhost:3000/images/1.json Λ։͍ͯ֬ೝͯ͠Έ·͠ΐ ͏ɻ ͳ͚Είϛοτ͠·͢ɻ -
API Խ ߘ࡞͍͖ͬͯ·͠ΐ͏ describe "POST /images.json" do it "create images"
do post "/images.json", image: { url: "http://example.com", title: "test" } expect(response.status).to be 201 end end spec Λ࣮ߦͯ͠Έ·͠ΐ͏ 302(ϦμΠϨΫτʣ͕ฦ͖ͬͯͯ͠·͍·͠ ͨͶɻ͔֬ϒϥβͰͷಈ࡞ߘޙɺindex ϖʔδͰϦμΠϨΫτ͢ΔΑ ͏ʹ͍ͯ͠·ͨ͠ɻAPI ͷ࣌ͦΕෆཁͰ͢Ͷɻࠓճ 201 ͚ͩฦͬͯ͘ ΔΑ͏ʹ͠·͢ɻ -
API Խ def create Image.create!(params.require(:image).permit(:title, :url)) respond_to do |format| format.html
{ redirect_to action: :index } format.json { head 201 } end end ͜ͷΑ͏ʹ format ʹΑͬͯॲཧΛ͚Δ͜ͱ͕ग़དྷ·͢ɻ -
API Խ ࣮ࡍʹίϚϯυϥΠϯ͔Β֬ೝͯ͠Έ·͠ΐ͏ $ curl -X POST -d "image[url]=http://example.com/&image[title]=test" http://localhost:3000/images.json
rails ͷϩάΛݟΔͱ͔ΔͷͰ͕͢ Can't verify CSRF token authenticity CSRF token ͕ແ͍ͨΊ͔Ε͍ͯ·͢ɻ -
API Խ ຊདྷͳΒ API Controller Λ͚ͯผͷೝূʢOAuth ʣʹ͠· ͕͢ɺͪΐͬͱॏ͗͢ΔͷͰࠓճ CSRF
token ͷνΣοΫΛແޮʹ ͠·͢ɻʢઈରʹ࣮ࡍͷ։ൃͰߦ͍͚ͬͯ·ͤΜʣ app/conrtollers/apprication_controller.rb ͷԼهΛॻ͖͑· ͢ - protect_from_forgery with: :exception + protect_from_forgery with: :null_session ͏ҰίϚϯυϥΠϯ͔Βୟ͍ͯΈͯͳ͚Είϛοτ͠·͢ -
API Խ ίϝϯτߘͰ͖ΔΑ͏ʹ͠·͢ spec/requests/comments_spec.rb require "rails_helper" RSpec.describe Comment, type: :request
do let(:image) { Image.create(url: "http://example.com/", title: "test") } describe "POST /images/:id/comments.json" do it "create comments" do post "/images/#{image.id}/comments.json", comment: { body: "test" } expect(response.status).to be 201 end end end -
API Խ app/conrtollers/comments_controller.rb class CommentsController < ApplicationController def create image
= Image.find(params[:image_id]) image.comments.create(params.require(:comment).permit(:body)) respond_to do |format| format.html { redirect_to image_path(image) } format.json { head 201 } end end end spec ͕௨Δ͜ͱΛ֬ೝͨ͠Βίϛοτ͠·͠ΐ͏ -
heroku Gemfile Λগ͠मਖ਼͠·͢ 1. sqlite3 Λ group :development, :test
ϒϩοΫͷதҠ ಈ 2. ԼهΛՃ group :production do gem 'rails_12factor' gem 'pg' end -
heroku $ bundle install ͯ͠ίϛοτ $ heroku create $
git push heroku master $ heroku run rake db:migrate $ heroku open ֬ೝͯ͠Έ·͠ΐ͏ -
Rails νϡʔτϦΞϧ1 Ζ͏ 1. ࠷ॳཧղͰ͖ͳͯ͘ 3 ճҐΔͱ͍ͩͿ֮͑Δ 2. Web ։ൃͲ͏Μ͚ͩͬͬͯࢥͬͨ࣌ͱ͔ʹ͞ΒͬͱΔ
ͱ·ͨࢥ͍ग़ͤΔ 1 http://railstutorial.jp/