質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.41%
Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

2回答

3925閲覧

カート内の商品をすべて削除する(空にする)機能を実装したい

pi-nattu

総合スコア61

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2022/09/26 13:38

編集2022/09/27 13:28

前提

課題でECサイトのケーキ屋ショッピングサイトを作成しています。
以下エラー文とコードです。エラー解決のためによろしくお願いします。

実現したいこと

  • カート内の商品を空にする機能の実装(カートの商品をすべて削除する)を実現したいです。

発生している問題・エラーメッセージ

NoMethodError in CartItems#index undefined method `destroy_all' for "/cart_items":String

該当のソースコード

ruby/cart_item/index_html.erb

1 <%= link_to "カートを空にする", cart_items_path.destroy_all, method: :delete, 2 "data-confirm" => "本当に空にしますか?" %><br>

ruby/cart_items_controller.rb

1 def destroy_all 2 CartItem.destroy_all 3 current_customer.cart_item.destroy_all 4 redirect_to cart_items_path, notice: 'カートが空になりました。' 5 end

試したこと

コントローラーの定義をカリキュラムや検索をしてていぎしてみました。
が、エラーの解決には至りませんでした。

補足情報(FW/ツールのバージョンなど)

ruby/cart_items/index_html.erb

1<h1>お客様カート</h1> 2<div> 3 <table> 4 <thead> 5 <tr> 6 <th>商品画像</th> 7 <th>商品名</th> 8 <th>単価(税込)</th> 9 <th>数量</th> 10 <th>小計</th> 11 <th></th> 12 </tr> 13 </thead> 14 <tbody> 15 <% @cart_items.each do |cart_item| %> 16 <tr> 17 <td><%= image_tag cart_item.item.get_item_image(100,100) %></td> 18 <td><%= cart_item.item.name %></td> 19 <td><%= cart_item.item.with_tax_price %>円</td> 20 <td><%= cart_item.amount %></td> 21 <td><%= cart_item.subtotal %>円</td> 22 <td><%= link_to "削除", cart_item_path(cart_item.id), method: :delete, "data-confirm" => "本当に削除しますか?" %></td> 23 </tr> 24 <% @total += cart_item.subtotal %> 25 <% end %> 26 </tbody> 27 </table> 28 <p>合計金額(税込)<%= @total %>円</p> 29 <%= link_to "買い物を続ける", items_path %><br> 30 <%= link_to "カートを空にする", cart_items_path.destroy_all, method: :delete, 31 "data-confirm" => "本当に空にしますか?" %><br> 32</div>

ruby/cart_items_controller.rb

1class CartItemsController < ApplicationController 2 def index 3 @cart_items = CartItem.all 4 @items = Item.all 5 @cart_item = @items.all 6 #合計金額の初期値は0円 7 @total = 0 8 end 9 10 11 def create 12 @cart_item = CartItem.find_by(item_id: params[:cart_item][:item_id]) 13 if @cart_item 14 #カートにitemが存在したらamountに新しいCart_itemのamountを足す 15 @cart_item.amount += CartItem.new(cart_item_params).amount 16 else 17 #カートにitemが無ければ新しく作成する 18 @cart_item = CartItem.new(cart_item_params) 19 end 20 #ログインcustomerのみ更新できるようにするため分岐の外に記述する 21 @cart_item.customer_id = current_customer.id 22 @cart_item.save! 23 redirect_to cart_items_path 24 end 25 26 27 def update 28 @cart_item = current_customer 29 @item = Item.all 30 @cart_item.update(cart_item_params) 31 redirect_to cart_items_path 32 end 33 34 def destroy 35 @cart_item = CartItem.find(params[:id]) 36 @cart_item.destroy! 37 redirect_to cart_items_path 38 end 39 40 def destroy_all 41 CartItem.destroy_all 42 current_customer.cart_item.destroy_all 43 redirect_to cart_items_path, notice: 'カートが空になりました。' 44 end 45 46 private 47 def cart_item_params 48 params.require(:cart_item).permit(:item_id, :amount) 49 end 50end

ruby/items_controller.rb

1class ItemsController < ApplicationController 2 def index 3 @items = Item.all 4 @genres = Genre.all 5 end 6 7 def show 8 @item = Item.find(params[:id]) 9 @cart_item = CartItem.new 10 end 11 12 def item_params 13 params.require(:item).permit( 14 :name, :item_image, :introduction, :item_price, :genre_id) 15 end 16end

追記▼

ruby/routes.rb

1Rails.application.routes.draw do 2 3 devise_for :admin, skip:[:registrations, :passwords] ,controllers: { 4 sessions: "admin/sessions" 5 } 6 7 get '/' => 'homes#top' 8 get '/about' => 'homes#about' 9 #customer用itemのパス 10 resources :items, only: [:index, :show] 11 namespace :admin do 12 get '/' => 'homes#top' 13 14 resources :items, only: [:new, :create, :index, :show, :edit, :update] 15 resources :genres, only: [:index, :edit, :create, :update] 16 end 17 18 #顧客用 19 devise_for :customers,skip: [:passwords], controllers: { 20 registrations: "public/registrations", 21 sessions: 'public/sessions' 22 } 23 24 #顧客情報関連 25 get '/customers/my_page' => 'customers#show' 26 get '/customers/infomation/edit' => 'customers#edit' 27 patch '/customers/infomation/up_date' => 'customers#update' 28 get '/customers/unsubscribe' => 'customers#unsubscribe' 29 patch '/customers/withdraw' => 'customers#withdraw' 30 #カート関連 31 32 delete '/cart_items/destroy_all' => 'cart_items#destroy_all', as: 'destroy_all_cart_items' 33 resources :cart_items, only: [:index, :update, :destroy, :create] 34 # 管理者用 35 36 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 37end 38

ruby/cart_items_html.erb変更しました

1 <%= link_to "カートを空にする", destroy_all_cart_items_path, method: :delete, "data-confirm" => "本当に空にしますか?" %><br>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2022/09/26 20:10 編集

HTMLが分かる人が全員RubyやRuby on Railsを分かるわけではないので 質問タグを追加してください。 ※むしろ本件HTMLの影響ってほぼないような
pi-nattu

2022/09/26 20:39

修正依頼ありがとうございます! つけたつもりが失念していました。申し訳ありません。
guest

回答2

0

cart_items_path.destroy_all これがおかしいです。
config/routes.rb に destroy_all は定義してありますか?定義してあれば destroy_all_cart_items_pathcart_items_destroy_all_path になるかと。cart_items でなく cart_item かもだな。rails routes で確認を。

投稿2022/09/27 00:31

winterboum

総合スコア23464

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

pi-nattu

2022/09/27 13:24

回答ありがとうございます! トライしてみたのですが、エラーの解決はされなかったのでスクールのメンターに聞き,ルーティングを見直したりwebpack/environment.jsやlayouts/application.html.erb等々見直しました。インスタンスの再起動も試みたのですが解決はできず、 Routing Error No route matches [GET] "/cart_items/destroy_all" と言うエラーに変わりました。 情報を追記してみましたが何かほかに案はありますでしょうか...?
winterboum

2022/09/27 15:02

link_to "カートを空にする", cart_items_path.destroy_all, method: :delete, ここ直してないですね エラーからみて、直してあるのかな。 現象の説明、エラーメッセージ と 載せてる code はきちんと同期してください No route matches [GET] "/cart_items/destroy_all" GET で DELETE でないですね。method: :delete, が効いていないな。 Rails のversionによってそこ手当が異なるので、、、、
pi-nattu

2022/09/28 12:20

返信ありがとうございます! スクールのメンターにも相談してみたところ解決できました。 自己解決で載せたBootstrapのインストールの際に起きたエラーでなければおそらくwinterboum様の言っていた記述で解決できていたのかもしれない、と思います。 エラーメッセージと記載コードの同期は今後気をつけていきたいと思います。申し訳ありませんでした。 回答してくださりありがとうございました!
guest

0

自己解決

今回のエラーに関して、どうやらBootstrap導入の際にstylesheetsファイル、application.scssが作成できていなかったために起きているエラーだったようです。
FontAwesomeやBootstrapなどのインストールでこういった問題があるとjsファイルのエラーによりDELETEメソッドが働かずGETメソッドになってしまうようです。
(ログアウトや商品単品の削除もGETメソッドになってエラーが起きていました)
このエラーはデベロッパー検証ツールのConsoleでもエラーが出るようなので、今後似たようなことがあったらそっちでも調べてみようと思いました。

最終的に

config/routes.rb

1Rails.application.routes.draw do 2 ~~~ 3 delete '/cart_items/destroy_all' => 'cart_items#destroy_all', as: 'destroy_all_cart_items' 4 resources :cart_items, only: [:index, :update, :destroy, :create] 5 # 管理者用 6 7 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 8end

ruby/cart_items/index.html.erb

1 <%= link_to "カートを空にする", destroy_all_cart_items_path, method: :delete, "data-confirm" => "本当に空にしますか?" %><br>

ruby/cart_items_controller

1def destroy_all 2 current_customer.cart_items.destroy_all 3 redirect_to cart_items_path, notice: 'カートが空になりました。' 4 end

で機能するようになりました。
今回のエラーで視野が少し広がったような気がします......

投稿2022/09/28 12:14

編集2022/09/28 12:33
pi-nattu

総合スコア61

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.41%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問