解決したいこと
Ruby on Railsで通知機能を実装しています。
通知機能実装中にビューに遷移しようとしたら下記エラーが発生しました。
発生している問題・エラー
NoMethodError in NotificationsController#index
undefined method `passive_notifications' for nil:NilClass
def index #current_userの投稿に紐づいた通知一覧 `@notifications=current_user.passive_notifications.page(params[:page])` #赤い部分 #@notificationの中でまだ確認していない(indexに一度も遷移していない)通知のみ @notifications.where(checked: false).each do |notification| notification.update_attributes(checked: true)
該当するソースコード
config/routes.rb
resources :notifications, only: :index
helpers/notifications_helper.rb
module NotificationsHelper def notification_form(notification) @visiter = notification.visiter @comment = nil your_post = link_to 'あなたの投稿', users_post_path(notification), style: 'font-weight: bold;' @visiter_comment = notification.comment_id # notification.actionがfollowかlikeかcommentか case notification.action when 'follow' tag.a(notification.visiter.name, href: users_user_path(@visiter), style: 'font-weight: bold;') + 'があなたをフォローしました' when 'like' tag.a(notification.visiter.name, href: users_user_path(@visiter), style: 'font-weight: bold;') + 'が' + tag.a('あなたの投稿', href: users_post_path(notification.post_id), style: 'font-weight: bold;') + 'にいいねしました' when 'comment' @comment = Comment.find_by(id: @visiter_comment)&.content tag.a(@visiter.name, href: users_user_path(@visiter), style: 'font-weight: bold;') + 'が' + tag.a('あなたの投稿', href: users_post_path(notification.post_id), style: 'font-weight: bold;') + 'にコメントしました' end end def unchecked_notifications @notifications = current_user.passive_notifications.where(checked: false) end end
controllers/notifications_controller.rb
class NotificationsController < ApplicationController def index #current_userの投稿に紐づいた通知一覧 @notifications=current_user.passive_notifications.page(params[:page]) #@notificationの中でまだ確認していない(indexに一度も遷移していない)通知のみ @notifications.where(checked: false).each do |notification| notification.update_attributes(checked: true) end end def destroy_all #通知を全削除 @notifications = current_user.passive_notifications.destroy_all redirect_to users_notifications_path end end
application.html.erb
<% if unchecked_notifications.any? %> <i class="fa fa-circle" style="color: gold;"></i> <% end %> <li> <%= link_to "NOTICE", notifications_path, class: "btn-default" %> </li>
_notification.html.erb
<% visitor = notification.visitor %> <% post = notification.post %> <div class="user-view clearfix "> <%= link_to users_user_path(notification.visitor) do %> <%= attachment_image_tag visitor, :profile_image, :fill,100,100, format: "jpeg", fallback: "no_image.jpg", size: "50x50", class:"profile-img-circle" %> <% end %> <%= notification_form(notification) %><span class="moderate-font"><%= " (#{time_ago_in_words(notification.created_at)} 前)" %></span> <br> <% if !@comment.nil? %> <p class="moderate-font text-center" style="color: #C0C0C0;"><%= @comment %></p> <% end %> </div>
notifications/index.html.erb
<h3 class="text-center">通知</h3> <%= link_to destroy_all_users_notifications_path, method: :delete do %> <i class="fas fa-trash" style="color: black;"></i> <h7 style="color: black;">全削除</h7> <% end %> <hr> <% if @notifications.exists? %> <div class="users-index"> <%= render @notifications %> </div> <% else %> <p>通知はありません</p> <% end %>
自分で試したこと
参考記事
https://qiita.com/ytoo14/items/2db1dd4fcd7945b980f7#er%E5%9B%B3
他の記事も調べ記述を変えてみたりもしましたが、解決には至りませんでした。
ご教授お願いします
まだ回答がついていません
会員登録して回答してみよう