2016-08-30 3 views
0

私は、経路:リソースである会話と部分的に会話していますが、ナビゲーションバーにこの部分をレンダリングしようとすると(アクティブな会話をすべて表示します)あなたのレールアプリのどこにでも部分をレンダリングする

undefined method `author' for nil:NilClass 

は、今私は

どこでもあなたのウェブサイト上であなたのパーシャルをレンダリングするための特別な方法はあり<%= render 'conversations/conversation' %>をやっていますか?

EDIT:コード

会話/ _conversation.html.erb

<div> 
    From <strong><%= conversation.author.email %></strong> to 
    <strong><%= conversation.receiver.email %> (<%#= conversation.receiver.online? ? 'online' : 'offline' %>)</strong> 
    <br> 
    <%= link_to 'View conversation', conversation_path(conversation) %> 
    <hr> 
</div> 

レイアウト/ _navigation.html.erb

<%= render 'conversations/conversation' %> 

経路

#Chat 
    resources :conversations 
    resources :personal_messages 
    resources :users 
    mount ActionCable.server => '/cable' 

EDIT2を提供

conversations_controller.rb

class ConversationsController < ApplicationController 
    before_action :set_conversation 
    before_action :check_participating!, except: [:index] 

    def index 
    @conversations = Conversation.participating(current_user).order('updated_at DESC') 
    respond_to do |format| 
     format.html 
     format.js 
    end 
    end 

    def show 
    @personal_message = PersonalMessage.new 
    @conversations = Conversation.participating(current_user) 
    end 

    private 

    def set_conversation 
    @conversation = Conversation.find_by(id: params[:id]) 
    end 

    def check_participating! 
    redirect_to root_path unless @conversation && @conversation.participates?(current_user) 
    end 
end 

ApplicationControllerに

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 

    before_action :authenticate_user! 

end 
+0

コードを入力できますか? –

+0

どのような方法が定義されていませんか? – Bek

+0

レンダリング部分: '会話/会話'、地元民:{会話:会話}? – Mingsheng

答えて

2

これを行うには、通常の方法がある:あなたが参照オブジェクトを提供していない場合は

render(partial: 'conversations/conversation', object: @conversation) 

結合データはありませんローカルのconversation変数に変更すると、nilが参照されます間違い。

+0

これと同じエラーが発生しています – Raidspec

+0

これは、定義されているインスタンス変数 '@ conversation'が存在することを前提としています。私はそれが事実かどうかわからない。これは単なる例です。会話記録がロードされていない場合は、このコードのどれも動作しません。 – tadman

+0

会話コントローラで定義されていますが、アプリケーションコントローラでは定義されていません。それはおそらくなぜですか? 多くの余分な作業をすることなく、ページ上で部分的にレンダリングできないのは奇妙です。 – Raidspec

関連する問題