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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

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

Q&A

解決済

1回答

300閲覧

通知機能(undefined method `passive_notifications' for nil:NilClass)

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

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

0グッド

0クリップ

投稿2022/05/13 17:02

解決したいこと

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

1 resources :notifications, only: :index

helpers/notifications_helper.rb

1module NotificationsHelper 2 def notification_form(notification) 3 @visiter = notification.visiter 4 @comment = nil 5 your_post = link_to 'あなたの投稿', users_post_path(notification), style: 'font-weight: bold;' 6 @visiter_comment = notification.comment_id 7 # notification.actionがfollowかlikeかcommentか 8 case notification.action 9 when 'follow' 10 tag.a(notification.visiter.name, href: users_user_path(@visiter), style: 'font-weight: bold;') + 'があなたをフォローしました' 11 when 'like' 12 tag.a(notification.visiter.name, href: users_user_path(@visiter), 13 style: 'font-weight: bold;') + 'が' + tag.a('あなたの投稿', href: users_post_path(notification.post_id), 14 style: 'font-weight: bold;') + 'にいいねしました' 15 when 'comment' 16 @comment = Comment.find_by(id: @visiter_comment)&.content 17 tag.a(@visiter.name, href: users_user_path(@visiter), 18 style: 'font-weight: bold;') + 'が' + tag.a('あなたの投稿', href: users_post_path(notification.post_id), 19 style: 'font-weight: bold;') + 'にコメントしました' 20 end 21 end 22 23 def unchecked_notifications 24 @notifications = current_user.passive_notifications.where(checked: false) 25 end 26end

controllers/notifications_controller.rb

1class NotificationsController < ApplicationController 2 def index 3 #current_userの投稿に紐づいた通知一覧 4 @notifications=current_user.passive_notifications.page(params[:page]) 5 #@notificationの中でまだ確認していない(indexに一度も遷移していない)通知のみ 6 @notifications.where(checked: false).each do |notification| 7 notification.update_attributes(checked: true) 8 end 9 end 10 11 def destroy_all 12 #通知を全削除 13 @notifications = current_user.passive_notifications.destroy_all 14 redirect_to users_notifications_path 15 end 16end 17

application.html.erb

1<% if unchecked_notifications.any? %> 2 <i class="fa fa-circle" style="color: gold;"></i> 3 <% end %> 4 <li> 5 <%= link_to "NOTICE", notifications_path, class: "btn-default" %> 6 </li>

_notification.html.erb

1<% visitor = notification.visitor %> 2<% post = notification.post %> 3 4<div class="user-view clearfix "> 5 <%= link_to users_user_path(notification.visitor) do %> 6 <%= attachment_image_tag visitor, :profile_image, :fill,100,100, format: "jpeg", fallback: "no_image.jpg", size: "50x50", class:"profile-img-circle" %> 7 <% end %> 8 <%= notification_form(notification) %><span class="moderate-font"><%= " (#{time_ago_in_words(notification.created_at)} 前)" %></span> 9 <br> 10 <% if !@comment.nil? %> 11 <p class="moderate-font text-center" style="color: #C0C0C0;"><%= @comment %></p> 12 <% end %> 13</div>

notifications/index.html.erb

1<h3 class="text-center">通知</h3> 2 <%= link_to destroy_all_users_notifications_path, method: :delete do %> 3 <i class="fas fa-trash" style="color: black;"></i> 4 <h7 style="color: black;">全削除</h7> 5 <% end %> 6 <hr> 7<% if @notifications.exists? %> 8 <div class="users-index"> 9 <%= render @notifications %> 10 </div> 11<% else %> 12 <p>通知はありません</p> 13<% end %>

自分で試したこと

参考記事
https://qiita.com/ytoo14/items/2db1dd4fcd7945b980f7#er%E5%9B%B3

他の記事も調べ記述を変えてみたりもしましたが、解決には至りませんでした。
ご教授お願いします

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

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

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

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

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

guest

回答1

0

ベストアンサー

current_user.passive_notifications
で置きていますから、current_user が nil ということは loginしていないのでは?

投稿2022/05/13 23:12

winterboum

総合スコア23329

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

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

退会済みユーザー

退会済みユーザー

2022/05/14 00:48

ご指摘の通りでした!ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問