2017-08-04 9 views
0

私は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を認識していないのですか、どこにでも動作するので、私はそれを定義しなければならないと確信しています。

+0

ここで '@ current_user'を設定していますか?そのコードを表示できますか?完全なエラーログとコントローラコードも表示してください。 – Gerry

+0

コントローラにエラーが発生したページにどのようなものが表示されますか? @current_userが正しく定義されていないと思います。 – Belder

+0

@BrandonElderそれも私の推測でした。それはレイアウトですが、私はそれがアプリコントローラから来ていると思います。奇妙なことに、そこにはない、私は彼らがどこかで定義されているかどうかわからないカップルの宝石を使った。私の懸念は、それを定義すると、current_userで動作する他のすべてのページでtypeを使いこなすことです。 – MaxLeoTony

答えて

1

navbarをレンダリングするたびに@current_userという変数を設定する必要があります。 Userモデルを持っているという理由だけで、@current_user変数に特定の時点で値が設定されているわけではありません。変数を設定するのはあなたの仕事です。 Railsは賢いですが、賢くはありません。

通常、controllerには@current_userという変数が設定されます。一部の人々はそのようなことのためにbefore_actionをやりたいと思っています。個人的には、私はしません。 ApplicationControllerbefore_actionを設定すると、@current_user変数がどこからでも利用できるようになります。

class ApplicationController < ActionController::Base 
    before_action :set_user 

    def index()  do_action_with_presentation    end 
    def new()  do_action_with_presentation    end 
    def create() do_cancellable_action_with_redirect  end 
    def show()  do_action_with_presentation    end 
    def edit()  do_action_with_presentation    end 
    def update() do_action_with_presentation    end 
    def delete() do_action_with_presentation    end 

private 

    def set_user 
    @current_user = User.find_by(something: :clever) 
    end 

end 

あなたは私の独特のRESTアクションを無視することができますようになりますRailsの4では

、。彼らは、ええ、独特です。

関連する問題