我正在使用mailboxer gem在我的rails应用程序的用户之间构建消息传递系统。出于某种原因,我收到了这个错误:
(未定义的方法`receipts_for'代表nil:NilClass)
也许是因为我应该在我的控制器或mailboxer.rb中的某处定义'receipts_for'?我试了几件......但不幸的是,他们都没有成功。
这是我的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'可以转到应用程序上任何用户的配置文件页面,并通过配置文件页面向他/她发送消息。所以这意味着我在我的用户模型的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'方法呢?或者我的代码还有其他问题?
非常感谢任何帮助!
谢谢!
答案 0 :(得分:2)
我会捎带@mu is too short所说的话。注意整个错误消息很重要。 “未定义的方法receipts_for for nil:NilClass”告诉你receipts_for
方法的接收者是零。 @conversation.receipts_for(current_user)
看起来是唯一使用receipts_for
的地方,所以我会通过确保为@conversation
分配现有ActiveRecord支持的会话对象的值来开始调试。
似乎有很多事情发生,所以我不知道如何为您提供快速解决方案。 ConversationsController#show
方法@conversation= current_user.mailbox.conversation.find(params[:id])
中的值分配看起来很麻烦。这告诉我,您正在寻找基于属于conversation
的{{1}}的{{1}},这可能是您想要的(在这种情况下,您需要拥有相应的mailbox
您的模型中定义了associations。
但是,由于链以current_user
结尾,我猜测不需要conversation.find(params[:id])
。或者,如果您的current_user.mailbox
实际上没有会话ID,那么也许这就是您需要关注的内容。
好消息是,如果你坚持byebug(或binding.pry,取决于你已经安装的内容),你可以找出如何定义params
@conversation
show
1}}方法并在您的视图中部分:
# In the controller:
def show
binding.pry
@conversation = current_user.mailbox.conversation.find(params[:id])
end
# in the view
<% binding.pry %>