我有一个部分路由是资源的会话:会话,但是当我尝试在导航栏中呈现这个部分时(列出所有活动的会话),我得到一个未定义的方法错误。
undefined method `author' for nil:NilClass
现在我正在做<%= render 'conversations/conversation' %>
是否有一种特殊方式可以在您的网站上呈现您的部分内容?
编辑:提供代码
会话/ _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 :(得分:2)
执行此操作的常规方法是:
conversation
如果您没有提供参考对象,则没有数据绑定到本地nil
变量,您会收到long batteryLevelPercent = beacon.getDataFields().get(0);
引用错误。