RailsによるアジャイルWebアプリケーション開発
第10章 タスクE:もっとスマートなカート
カートを空にする、DELETE 部分、IDを渡す必要がないのでroutingを変えた方がいいんじゃないだろうか。
- config/routes.rb
# : resources :carts, except: [:destroy] do delete :destroy, on: :collection end # :
- app/views/carts/show.html.haml
- if notice %p#notice= notice .cart_title カート %table %tbody - @cart.line_items.each do |item| %tr %td = item.quantity × %td= item.product.title %td.item_price= number_to_currency item.total_price %tr.total_line %td{ colspan: "2" } 合計 %td.total_cell= number_to_currency @cart.total_price = button_to 'カートを空にする', carts_path, method: :delete, confirm: '本当によいですか?'
- spec/controllers/carts_controller_spec.rb
require 'rails_helper' RSpec.describe CartsController, type: :controller do describe 'DELETE destroy' do before do @cart = create(:cart) session[:cart_id] = @cart.id end it { expect { delete :destroy }.to change(Cart, :count).by(-1) } end end
今日の感想
最近停滞してたので頑張らないと。