前提・実現したいこと
@cart
配列より取り出した各要素に削除ボタンを追加した際に、該当する各要素をボタンを押して削除出来るようにしたい。
発生している問題・エラーメッセージ
@cart.update
の場合
NoMethodError in CartController#update undefined method `update' for #<Array:0x00007f8d82d455f0>
@cart.delete([:id])
の場合
変化なし。
terminalでは以下のような形になります。
delete/delete_atメソッドを用いても、railsの方でエラーは起きないのですが、削除した内容も反映されないといった具合になります。
Started PATCH "/cart/1" for 127.0.0.1 at 2018-08-25 16:19:19 +0900 Processing by CartController#update as HTML Parameters: {"authenticity_token"=>"uCSoRxnlGlRIT/Z4YHFcppIA91GQpnIqbLXMtTZnOnsFi+1X4TNMGN49eBZO+qzgd+kCcZovUw48uosD2pdLjQ==", "id"=>"1"} Item Load (0.4ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] ↳ app/controllers/cart_controller.rb:26 CACHE Item Load (0.0ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] ↳ app/controllers/cart_controller.rb:26 Item Load (0.4ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]] ↳ app/controllers/cart_controller.rb:26 Redirected to http://localhost:3000/cart Completed 302 Found in 5ms (ActiveRecord: 0.8ms)
該当のソースコード
cart.controller.rb
ruby
1class CartController < ApplicationController 2 # before_action :set_cart, only: [:update] 3 def index 4 if session[:cart] != nil 5 session[:cart].sort! 6 @cart = session[:cart].map { |item_id| Item.find(item_id) } 7 end 8 end 9 10 def create 11 session[:cart] = [] unless session[:cart] 12 session[:cart] << params[:item_id] 13 redirect_to top_path, notice: 'カートに商品を入れました' 14 end 15 16 def update 17 # 配列の中から特定の要素を削除 18 # session.delete([:item_id]) 19 # @cart.update 20 # session[:cart].delete([:id]) 21 # @cart.delete([:id]) 22 @cart = session[:cart].map { |item_id| Item.find(item_id) } 23 @cart.delete 24 redirect_to cart_index_path 25 # session[:cart] 26 # =>["1", "1", "3", "3"] 27 #@cart 28 # => [#<Item id: 1, name: "1", description: "1", price: 1, created_at: "2018-08-23 09:53:01", updated_at: "2018-08-23 09:53:01", image: "masamune.jpg">, #<Item id: 1, name: "1", description: "1", price: 1, created_at: "2018-08-23 09:53:01", updated_at: "2018-08-23 09:53:01", image: "masamune.jpg">, #<Item id: 3, name: "2", description: "2", price: 2, created_at: "2018-08-23 09:54:19", updated_at: "2018-08-23 09:54:19", image: "transcendence.jpg">, #<Item id: 3, name: "2", description: "2", price: 2, created_at: "2018-08-23 09:54:19", updated_at: "2018-08-23 09:54:19", image: "transcendence.jpg">] 29 end 30 31 def destroy 32 33 reset_session 34 # @cart = session[:cart].map { |item_id| Item.find(item_id) } 35 # @cart.destroy 36 # @cart =Item.find(@item_id) 37 # @cart.destroy 38 redirect_to cart_index_path 39 end 40 41 # private 42 # # Use callbacks to share common setup or constraints between actions. 43 # def set_cart 44 # @cart = session[:cart].map { |item_id| Item.find(item_id) } 45 # end 46 # 47 # # Never trust parameters from the scary internet, only allow the white list through. 48 # def cart_params 49 # params.require(:cart) 50 # end 51end
index.html.rb
ruby
1<tbody> 2<h1>注文一覧</h1> 3<% sum = 0 %> 4<% if @cart.blank? %> 5<%= "カートは空です" %><br> 6<% else %> 7<% @cart.each do |item| %> 8<tr> 9<td>商品番号: <%= item.id %></td> 10<td>価格: <%= item.price %>円</td><br> 11<% sum += item.price %> 12<%= button_to "Clear Product", cart_path(item), method: :patch, data: { confirm: 'Are you sure?' } %><br> 13<% end %><br> 14<!-- 合計金額: <%= sum %>円<br> --> 15 16</tr> 17</tbody> 18<br> 19<% end %>
試したこと
ソースコードでコメントアウトしている内容になります。
# session.delete([:item_id]) # @cart.update # session[:cart].delete([:id]) # @cart.delete([:id])
補足情報(FW/ツールのバージョンなど)
Rails 5.2
追記
index.html.erbにて
ruby
1<% if @cart.blank? %> 2<%= "カートは空です" %><br> 3<% else %> 4<% @cart.each do |item| %> 5<tr> 6<td>商品番号: <%= item.id %></td> 7<td>価格: <%= item.price %>円</td><br> 8<% sum += item.price %> 9<%= button_to "Clear Product", cart_path(item), method: :patch, data: { confirm: 'Are you sure?' } %><br> 10<% end %><br> 11
cart_path(item)とすることで送られているデータはcart.controller def index
にて定義された @cart = session[:cart].map { |item_id| Item.find(item_id) }
の @cart
ではなく、 def update
にて定義した @cart
が入ること。
そしてCartはモデルではない為に、destroy,update
などのメソッドは継承されていない為使用不可なこと。
しかし @cart = Item.find(params[:id])
と記述を変えることで、 @cart.destroy
を実行するとItemモデルのdestroyを実行することで再び大元のデータを消してしまうこと。
その代わり、 @cart
は削除したいデータそのものを引っ張ってきていること。。
ここまでが分かりました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/26 05:16 編集
2018/08/27 05:36
2018/08/31 06:51