邮箱宝石,"未定义的方法`参与者'为零:NilClass"

时间:2015-07-25 20:13:48

标签: ruby-on-rails mailboxer

@conversation似乎没有加载实际的会话。加载单个会话时出现nilClass错误,我不确定原因。我觉得我错过了一些愚蠢的事情。

这是错误(在show视图中对任何对话的使用都会持续存在):

undefined method `participants' for nil:NilClass

Extracted source (around line #1):

<% conversation.participants.each do |participant| %>
  <% unless participant == current_user %>
    <%= gravatar_for participant %>
  <% end %>
<% end %>

Rails.root: /home/alex/workspace/meetexplore_app

Application Trace | Framework Trace | Full Trace
app/views/conversations/_participants.html.erb:1:in     `_app_views_conversations__participants_html_erb___865731980245228929_7035    9899418680'
app/views/conversations/show.html.erb:4:in    `_app_views_conversations_show_html_erb__775537665966091139_41963900'

我的对话/ show.html.erb文件:

    <% page_header "Conversation" %>
    <p>Chatting with
      <%= render 'conversations/participants', conversation: @conversation %>    
    </p>

    <div class="panel panel-default">
      <div class="panel-heading"><%= @conversation.subject %></div>

      <div class="panel-body">
        <div class="messages">
          <% @conversation.receipts_for(current_user).each do |receipt| %>
            <div class="media">
              <% message = receipt.message %>
              <div class="media-left">
                <%= gravatar_for message.sender, 45, message.sender.name %>
              </div>

          <div class="media-body">
            <h6 class="media-heading"><%= message.sender.name %>
              says at <%= message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></h6>
            <%= message.body %>
          </div>
        </div>
      <% end %>
    </div>
  </div>
</div>`

我的_participants.html.erb部分:

    <% conversation.participants.each do |participant| %>
  <% unless participant == current_user %>
    <%= gravatar_for participant %>
  <% end %>
<% end %>

然而这个指数有效:

<% page_header "Your Conversations" %>
 <p><%= link_to 'Start conversation', new_message_path, class: 'btn btn-lg btn-primary' %></p>

<ul class="list-group">
  <%= render partial: 'conversations/conversation', collection: @conversations %>
</ul>

<%= will_paginate %>

_conversation.html.erb partial:

<li class="list-group-item clearfix">
  <%= link_to conversation.subject, conversation_path(conversation) %>

  <p><%= render 'conversations/participants', conversation: conversation %></p>

    <p><%= conversation.last_message.body %>
      <small>(<span class="text-muted"><%= conversation.last_message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></span>)</small></p>

</li>

1 个答案:

答案 0 :(得分:0)

所以我错过了before_action和函数get_conversation来实际找到具有正确id的对话... woops!