我正在我的RoR应用程序上运行聊天应用程序。我希望能够从我的布局/应用程序.html.erb中呈现用户聊天会话,以便让用户在所有页面上进行聊天,但只有当我在用户控制器类UsersController < ApplicationController下运行索引操作方法时,聊天才能正常工作。当我在views/users/index.html.erb上聊天时,它运行得很好。为了使其在所有页面中都可用,我在views/users下创建了一个名为_chatlist.html.erb的呈现表单,其中包含以下行:
并在我的layouts/application.html.erb正文中将此表单呈现为:<%= render partial:'users/chatlist‘%>当我这样做时,我得到了这个错误:未定义的方法’`any?‘对于nil:NilClass。
我该如何解决这个零问题呢?任何帮助都将不胜感激。
发布于 2016-10-11 17:54:19
您会收到该错误,因为@conversations
未初始化。如果你想让它在所有页面上都可用,你必须编写一个方法并在application_controller.rb中调用它。您的application_controller.rb
应该如下所示。
class ApplicationController < ActionController::Base
before_filter :load_conversatios
def load_conversatios
# load you conversation here
@conversations = Conversation.all
end
end
我认为这不是在所有页面上加载所有对话的好方法。你必须让它在ajax调用中可用。
https://stackoverflow.com/questions/39983338
复制相似问题