NoMethodError(针对nil:NilClass的未定义方法`email'):/操作邮件程序Rails

时间:2018-07-19 13:28:18

标签: ruby-on-rails actionmailer

当有人在我的网站上收到消息时,我尝试使用ActionMailer自动发送电子邮件通知。我使用Devise进行用户身份验证。

测试邮件程序时,我在终端中始终收到此消息:

NoMethodError (undefined method `email' for nil:NilClass):
app/mailers/app_mailer.rb:6:in `new_message'
app/controllers/messages_controller.rb:21:in `create'

这是我的文件: app / mailers / app_mailer.rb

class AppMailer < ActionMailer::Base

default from: "no-reply@adventurer.com"

  def new_message(user)
    @user = user
    mail(to: @user.email, subject: 'Vous avez un nouveau message')
end

end

app / controllers / messages_controller.rb

def create
        @message = @conversation.messages.new(message_params)
        @messages = @conversation.messages.order("created_at DESC")

        if @message.save

          AppMailer.new_message(@user).deliver_now
          respond_to do |format|
            format.js            
          end
        end        
    end

有人可以帮助我吗?我不明白为什么它不能识别用户上存在的.email方法。 谢谢!

编辑: 我在session.rb

class Conversation < ApplicationRecord

  belongs_to :sender, foreign_key: :sender_id, class_name: 'User'

  belongs_to :recipient, foreign_key: :recipient_id, class_name: 'User'

  has_many :messages, dependent: :destroy

  validates_uniqueness_of :sender_id, scope: :recipient_id

  scope :involving, -> (user) do
       where("conversations.sender_id = ? OR conversations.recipient_id = ?", user.id, user.id)
   end

  scope :between, -> (sender_id, recipient_id) do
       where("(conversations.sender_id = ? AND conversations.recipient_id = ?) OR (conversations.sender_id = ? AND conversations.recipient_id = ?)", sender_id, recipient_id, recipient_id, sender_id)

   end

end

在messages_controller.rb

def create
        @message = @conversation.messages.new(message_params)
        @messages = @conversation.messages.order("created_at DESC")

        if @message.save

      AppMailer.new_message(@user).deliver_now
      respond_to do |format|
        format.js            
      end
    end        
end

在我的app_mailer.rb

class AppMailer < ActionMailer::Base
  default from: "no-reply@adventurer.com"

  def new_message(user)
    @user = User.find(@conversations.recipient_id)
    mail(to: @user.email, subject: 'Vous avez un nouveau message')
  end
end

当我尝试发送消息时,我在终端中收到此消息:NoMethodError(nil:NilClass的未定义方法`recipient_id')

再次感谢您的帮助;)

编辑 messages_controller.rb

def create
        @message = @conversation.messages.new(message_params)
        @messages = @conversation.messages.order("created_at DESC")

        if @message.save
          @user = User.find(@conversation.recipient_id)    
          AppMailer.new_message(@user).deliver_now
          respond_to do |format|
            format.js            
          end
        end        
    end

app_mailer.rb

class AppMailer < ActionMailer::Base
  default from: "no-reply@adventurer.com"

  def new_message(user)
    @user = User.find(@conversation.recipient_id)    
     mail(to: @user.email, subject: 'Vous avez un nouveau message')

  end
end

2 个答案:

答案 0 :(得分:0)

您的@user对象未初始化。那就是为什么您收到此错误。我猜您可以从@conversation对象中对其进行初始化。假设@conversation个对象包含sender_idreceiver_id或类似的对象。尝试通过@user@user = User.find(@conversation.receiver_id)初始化@user = User.find(@conversation.sender_id),具体取决于哪个是发送方,另一个是接收方。希望对您有帮助

答案 1 :(得分:0)

由于我的声誉低下,我无法评论您的最后解释。 @user = User.find(@conversations.recipient_id)将此行转到create方法。这次,您通过未初始化的@conversations对象得到了同样的错误。