現在、railsで通知機能を実装していて、Routingの実装方法が分かりません。具体的には、notificationリソースにdestroy_allアクションをcollectionとして追加したいのですが、書き方が分かりません。次のように書いており、index以外の不要なnotificationリソースのアクションが生成されています。
resources :notifications, only: :index resources :notifications do collection do delete 'destroy_all' end end
また、ルーティングが原因なのか不明ですが、destroy_all メソッドを実行すると、図1のようなshowアクションを探索するエラーが発生します。コントローラーは図2のようにindexアクションへのリダイレクトを期待します。何故showアクションが探索されるのでしょうか。図3は該当するviewです。
Unknown action The action 'show' could not be found for NotificationsController
class NotificationsController < ApplicationController before_action :sign_in_required, only: :index def index #current_userの投稿に紐づいた通知一覧 @notifications = current_user.passive_notifications #@notificationの中でまだ確認していない(indexに一度も遷移していない)通知のみ @notifications.where(checked: false).each do |notification| notification.update(checked: true) end end def destroy_all #通知を全削除 @notifications = current_user.passive_notifications.destroy_all redirect_to notifications_path end end
view
1 2<div class="grid grid-cols-1 justify-center bg-lime-100 px-3 py-3"> 3 <div class='bg-lime-200'> 4 <% if @notifications.exists? %> 5 <%= link_to destroy_all_notifications_path, method: :delete do %> 6 <p>全て削除</p> 7 <% end %> 8 <%= render @notifications %> 9 <% else %> 10 <div class="flex w-full flex-col justify-center items-center mb-48"> 11 <span class ="relative"> 12 <div class="h-36 w-36 bg-white rounded-full -ml-1.5 "></div> 13 <%= image_tag 'ojigi_animal_inu.png', class:' absolute top-6 left-4 z-10 w-24 h-28 p-4' %> 14 </span> 15 <p class="font-medium py-2">新着の通知はありません。</p> 16 </div> 17 <% end %> 18 </div> 19</div> 20
情報の不備などございましたらご指摘ください。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー