【问题标题】:Rails 4 undefined method `any?' for nil:NilClassRails 4 未定义的方法“任何?”对于零:NilClass
【发布时间】:2017-02-20 08:30:32
【问题描述】:

我正在我的 RoR 应用程序上运行一个聊天应用程序。我希望能够从我的 layouts/application.html.erb 呈现用户聊天对话,以便在所有页面上进行用户聊天对话,但只有当我在我的用户控制器 [class UsersController

并在我的 layouts/application.html.erb 正文中将此表单呈现为: 当我这样做时,我收到此错误:未定义的方法“任何?”对于零:NilClass。

我该如何解决这个零问题?任何帮助将不胜感激。

【问题讨论】:

  • 抱歉,我的表单 _chatlist.html.erb 中的代码行是
  • 您需要将@conversations 传递给您的部分。尝试<%= render partial: 'users/chatlist', conversations: @conversations %> 并在部分中切换到<% if conversations.any? %>

标签: ruby-on-rails-4


【解决方案1】:

您收到该错误是因为 @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 调用使其可用。

【讨论】:

  • 聊天对话可通过 ajax 调用获得。我按照本教程进行操作。 josephndungu.com/tutorials/…
  • 这个想法是当用户在我的应用程序中从一个页面移动到另一个页面时让聊天对话可用。现在,如果用户在 views/users/index.html.erb 中打开了一个聊天框并移动到另一个页面,则对话聊天框将关闭。
猜你喜欢
  • 1970-01-01
  • 2012-03-15
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多