Rails未定义的方法'每个'为零:NilClass

时间:2014-04-16 22:44:02

标签: ruby-on-rails

我在我的rails应用程序中设置内部邮件系统,但我无法将邮件实际发送给其他用户。

class User < ActiveRecord::Base
  # messages and conversations
  has_many :user_conversations
  has_many :conversations, through: :user_conversations
  has_many :messages

class UserConversation < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation
  before_create :create_user_conversations

  accepts_nested_attributes_for :conversation

  delegate :subject, to: :conversation
  delegate :users, to: :conversation

  attr_accessor :to

  private
    def create_user_conversations
       to.each do |recip|
        recipient = User.find(recip)
        UserConversation.create(user_id: recip, conversation_id: 1)
       end
    end
end

class Conversation < ActiveRecord::Base
  has_many :user_conversations
  has_many :users, through: :user_conversations
  has_many :messages
  accepts_nested_attributes_for :messages

class Message < ActiveRecord::Base
  belongs_to :user_conversation
  belongs_to :user

这是我的user_conversation_controller:

class UserConversationsController < ApplicationController

  def new
    @user = User.find(params[:user_id])
    @conversation = @user.user_conversations.build
    @conversation.build_conversation.messages.build
  end

  def create
    @conversation = UserConversation.new(conversation_params)
    @conversation.user = current_user
    @conversation.conversation.messages.first.user = current_user
    if @conversation.save
      redirect_to user_conversation_path(current_user, @conversation)
    else
      flash[:error] = "There was an error"
      render 'new'
    end
  end

  private
    def conversation_params
      params.require(:user_conversation).permit(:to => [],
                                                conversation_attributes: [:subject, 
                                                messages_attributes: [:body]])
    end

错误来自UserConversation模型中的create_user_conversations方法。当我尝试运行时

to.each do |recip|

我得到一个&#34;未定义的方法&#39;每个&#39;为零:NilClass&#34;错误。然而,&#34; to&#34;数组中有一个值,在这种情况下参数如下所示:

{"utf8"=>"✓",
 "user_conversation"=>{"to"=>["2"],
 "conversation_attributes"=>{"subject"=>"Hey",
 "messages_attributes"=>{"0"=>{"body"=>"hey"}}}},
 "commit"=>"Create User conversation",
 "user_id"=>"1"} 

关于为什么该阵列没有正确传递的任何想法?感谢。

2 个答案:

答案 0 :(得分:0)

您将to定义为attr_accessor,它将为实例变量@to创建get / set方法。您在私有方法to中使用create_user_conversations作为本地变量。这解释了nil:NilClass错误。

尝试将局部变量更改为实例变量。

答案 1 :(得分:0)

我通过向我的user_conversations表添加recipients_id列来解决我的问题,然后在我的UserConversations控制器中我能够做到

def create
  @conversation = UserConversation.new(user_conversation_params)
  @conversation.user = current_user
  @conversation.conversation.messages.first.user_id = current_user.id
  if @conversation.save
    UserConversation.recipient_id = @conversation.recipient_id
    redirect_to user_conversation_path(current_user, @conversation)
    create_user_conversations
  else
    flash[:error] = "There was an error"
    render 'new'
  end
end

使用私有方法create_user_conversations也在我的UserConversations控制器中:

def create_user_conversations
  UserConversation.recipient_id.each do |recip|
    recipient = User.find(recip)
    UserConversation.create(user: recipient, conversation: @conversation.conversation)
  end
end

我怀疑这是最优雅的方式,但它至少可以完成工作。