(nil:NilClass 的未定义方法 `receipts_for' - MailBoxer Gem Rails

(undefined method `receipts_for' for nil:NilClass - MailBoxer Gem Rails

我正在使用邮箱 gem 为我的 rails 应用程序构建用户之间的消息传递系统。出于某种原因,我收到此错误:

(nil:NilClass 的未定义方法 `receipts_for')

也许是因为我应该在我的控制器或 mailboxer.rb 中的某处定义 'receipts_for'?我尝试了一些东西.. 但不幸的是 none 其中成功了。

这是我的routes.rb:

Rails.application.routes.draw do
  root   'static_pages#home'
  get    '/help',    to: 'static_pages#help'
  get    '/about',   to: 'static_pages#about'
  get    '/contact', to: 'static_pages#contact'
  get    '/signup',  to: 'users#new'
  get    '/login',   to: 'sessions#new'
  get    'search/index'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  resources :users do
    member do
      get :following, :followers
    end
  end
  resources :conversations do
    resources :messages
  end
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
  resources :microposts,          only: [:create, :destroy]
  resources :relationships,       only: [:create, :destroy]
  resources :searches
end

这是我的对话控制器:

class ConversationsController < ApplicationController

  def show
    @conversation = current_user.mailbox.conversation.find(params[:id])
  end


  def new
    @recipients = User.find(params[:user_id])
  end

  def create
    recipient = User.find(params[:user_id])
    receipt = current_user.send_message(recipient, params[:body])
    redirect_to conversation_path(receipt.conversation)
  end

end

这是我的messages_controller:

class MessagesController < ApplicationController
  before_action :set_conversation

  def create
    receipt = current_user.send_message(@conversation, body)
    redirect_to receipt.conversation
  end

  private

  def set_conversation
    @conversation = current_user.mailbox.conversations.find(params[:conversation_id])
  end

end

我正在构建的消息系统正是这样一个消息系统,'current_user' 可以转到应用程序上任何用户的个人资料页面,并通过个人资料页面向 him/her 发送消息。所以这意味着我在用户模型的 show.html.erb 中呈现 show.html.erb 的对话模型。这是两个视图的代码:

show.html.erb - 用户模型:

<% provide(:title, @user.name) %>
<div class="row">
  <aside class="col-md-4">
    <section>
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </section>
    <section class="stats">
      <%= render 'shared/stats' %>
    </section>
      <h6>
        <%= @user.gender %>
      </h6>
    <%= render "conversations/conversation" %>
  </aside>
  <div class="col-md-8">
    <%= render 'follow_form' if logged_in? %>
    <% if @user.microposts.any? %>
      <h3>Microposts (<%= @user.microposts.count %>)</h3>
      <ol class="microposts">
        <%= render @microposts %>
      </ol>
      <%= will_paginate @microposts %>
    <% end %>
  </div>
</div>

_conversation.html.erb - 显示对话模型的视图。

<% @conversation.receipts_for(current_user).each do |receipt| %>
    <div>
        <%= receipt.message.body %>
    </div>
<% end %>


<%= form_tag conversation_messages_path(@conversation), method: post do  %>
    <div>
        <%= text_area_tag :body %>
    </div>

    <%= submit_tag %>
<% end %>

那么我该如何为我的邮箱定义 'receipts_for' 方法呢?还是我的代码有其他问题?

如有任何帮助,我们将不胜感激!

谢谢!

我会借鉴 @mu is too short 所说的内容。注意整个错误消息很重要。 “undefined method receipts_for for nil:NilClass”告诉你 receipts_for 方法的接收者是 nil。 @conversation.receipts_for(current_user) 看起来是唯一使用 receipts_for 的地方,所以我会通过确保 @conversation 被分配到现有的 ActiveRecord 支持的对话对象的值来开始调试。

似乎发生了太多事情,所以我不知道如何为您提供快速修复。 ConversationsController#show 方法中的赋值,@conversation= current_user.mailbox.conversation.find(params[:id]) 看起来很麻烦。这向我表明您正在寻找基于属于 current_usermailboxconversation,这可能是您想要的(在这种情况下,您需要有适当的associations 在您的模型中定义)。

但是,由于链以 conversation.find(params[:id]) 结尾,我猜测不需要 current_user.mailbox。或者,如果您的 params 实际上没有对话 ID,那么也许这就是您需要关注的。

好消息是,如果您将 byebug (or binding.pry 粘贴在 show 方法的顶部,您或许可以弄清楚如何定义 @conversation,具体取决于您安装的内容)和在您看来部分:

# In the controller:

  def show
    binding.pry
    @conversation = current_user.mailbox.conversation.find(params[:id])
  end


# in the view
<% binding.pry %>