私はnavbarに通知を表示しており、ページの約半分で動作します。 他の半分では、私は以前は扱っていなかったNilClassエラーが発生します。しかし、私は@ current_user.notifications.eachがユーザと通知モデルのために十分であるという印象を受けています。 明らかにそうではありません。通知未定義のメソッドnilのための `notifications ':NilClass
<div class="dropdown nav-button notifications-button hidden-sm-down">
<a class="btn btn-secondary dropdown-toggle" href="#" id="notifications-dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i id="notificationsIcon" class="fa fa-bell-o" aria-hidden="true"></i>
</a>
<div class="dropdown-menu notification-dropdown-menu" aria-labelledby="notifications-dropdown">
<h6 class="dropdown-header">Notifications</h6>
<div id="notificationsContainer" class="notifications-container">
<% @current_user.notifications.each do |notification| %>
<% if notification.notice_type == "post" %>
<%= link_to "Someone commented on your post", notification.post %>
<% end %>
<br>
<% end %>
<hr>
<%= link_to "All Notifications", notifications_path %>
<br>
<br>
</div>
</div>
</div>
と
ナビゲーションバーのセクションユーザーモデルは、私はこのチュートリアル(https://www.devwalks.com/lets-build-instagram-part-6-notifications/)で働いていたが、道に沿っていくつかのことを変更し
has_many :notifications, dependent: :destroy
通知モデル
class Notification < ApplicationRecord
belongs_to :notified_by, class_name: 'User'
belongs_to :user
belongs_to :post, optional: true
validates :user_id,
:notified_by_id,
:identifier,
:notice_type,
presence: true
end
が含まれています。例えば、私はHAMLを使いたくなかった。 とにかく、なぜそれがcurrent_userを認識していないのですか、どこにでも動作するので、私はそれを定義しなければならないと確信しています。
ここで '@ current_user'を設定していますか?そのコードを表示できますか?完全なエラーログとコントローラコードも表示してください。 – Gerry
コントローラにエラーが発生したページにどのようなものが表示されますか? @current_userが正しく定義されていないと思います。 – Belder
@BrandonElderそれも私の推測でした。それはレイアウトですが、私はそれがアプリコントローラから来ていると思います。奇妙なことに、そこにはない、私は彼らがどこかで定義されているかどうかわからないカップルの宝石を使った。私の懸念は、それを定義すると、current_userで動作する他のすべてのページでtypeを使いこなすことです。 – MaxLeoTony