简体   繁体   English

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

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

I'm using the mailboxer gem to build a messaging system between users for my rails application. 我正在使用邮箱管理器gem为我的Rails应用程序在用户之间构建消息传递系统。 For some reason, I'm getting this error: 由于某种原因,我收到此错误:

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

Maybe it's because I should define 'receipts_for' somewhere in my controller or in the mailboxer.rb? 也许是因为我应该在控制器或mailboxer.rb中的某个位置定义“ receipts_for”? I tried a few things.. but unfortunately none of them were successful. 我尝试了一些方法..但是不幸的是,它们都没有成功。

This is my routes.rb: 这是我的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 

This is my conversations controller: 这是我的对话控制器:

 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 

This is my messages_controller: 这是我的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 

The messaging system I'm building precisely, is a messaging system where the 'current_user' can go to the profile page of any user on the application and message him/her via the profile page. 我正在精确构建的消息传递系统是一个消息传递系统,其中“ current_user”可以转到应用程序上任何用户的个人资料页面,并通过个人资料页面向他/她发送消息。 So this means that I'm rendering show.html.erb of the conversation model inside the show.html.erb of my users model. 因此,这意味着我正在用户模型的show.html.erb中渲染对话模型的show.html.erb。 Here is the code of both views: 这是两个视图的代码:

show.html.erb - Users Model: 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 - show view of the conversations model. _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 %> 

So how exactly can I define the 'receipts_for' method for my mailboxer? 那么,如何为我的邮箱管理员定义'receipts_for'方法呢? Or is something else wrong with my code? 还是我的代码有其他问题?

Any help would be much appreciated! 任何帮助将非常感激!

Thanks! 谢谢!

I'll piggy-back on what @mu is too short said. 我会背诵@mu太短的内容 It's important to pay attention to the whole error message. 注意整个错误消息很重要。 “undefined method receipts_for for nil:NilClass” is telling you that the receiver of the receipts_for method is nil. “对零未定义的方法receipts_for:NilClass”告诉你,的接收器receipts_for方法是零。 @conversation.receipts_for(current_user) looks to be the only place where receipts_for is used, so I would start debugging by making sure @conversation is assigned the value of an existing ActiveRecord-backed conversation object. @conversation.receipts_for(current_user)看起来是只住的地方receipts_for使用,所以我会通过确保启动调试@conversation分配现有的ActiveRecord的支持谈话对象的值。

There seems to be too a lot going on, so I don't know how to offer you a quick fix. 似乎发生了太多事情,因此我不知道如何为您提供快速修复。 The value assignment in your ConversationsController#show method, @conversation= current_user.mailbox.conversation.find(params[:id]) looks cumbersome. 您的ConversationsController#show方法@conversation= current_user.mailbox.conversation.find(params[:id])的值分配看起来很麻烦。 That suggests to me that you're looking for a conversation based on a mailbox belonging to the current_user , which might be what you want (in which case, you need to have the appropriate associations defined in your models). 这对我来说意味着您正在寻找一个基于属于current_usermailboxconversation ,这可能是您想要的(在这种情况下,您需要在模型定义适当的关联 )。

However, since the chain ends with conversation.find(params[:id]) , I'm guessing that current_user.mailbox is not needed. 但是,由于该链以conversation.find(params[:id])结尾,所以我猜想不需要current_user.mailbox Alternatively, if your params don't actually have a conversation id, then maybe that's what you need to focus on. 另外,如果您的params实际上没有对话ID,那么也许这就是您需要关注的重点。

The good news is you can probably figure out how to define @conversation if you stick byebug (or binding.pry , depending on what you have installed) at the top of your show method and in your view partial: 好消息是,你也许可以弄清楚如何定义@conversation如果你坚持byebug (或binding.pry ,这取决于您所安装的)在你的顶部show方法和您的视图部分:

# In the controller:

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


# in the view
<% binding.pry %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM