ruby on rails-Mailer预览:未定义的局部变量或方法



我是一名新手Rails程序员,试图设置邮件预览,但当访问服务器上的预览时,我得到了错误:

undefined local variable or method `email' for #<InvitationMailerPreview:0x000000051a77c8>

在test/mailers/previews/invitation_mailer_preview.rb中,我有下面的方法。错误消息指向invitation =行。Invitation表中未包含名为email的列。

def invitation
  invitation = Invitation.where(email != nil).first
  InvitationMailer.invitation_verified(invitation)
end

In-app/mailers/invitation_mailer.rb(本帖简称(:

def invitation(invitation)
  @invitation = invitation
  mail to: invitation.invitee.email
end

你知道我做错了什么吗?是什么导致了这个错误?

where子句需要传递一个散列。

要查找具有nil电子邮件的邀请:

invitation = Invitation.where(email: nil).first

要查找没有的邀请,请发送nil电子邮件:

invitation = Invitation.where.not(email: nil).first

相当于:

invitation = Invitation.where('email IS NOT NULL').first

Invitation.where(email != nil)无效。您应该将其更改为:Invitation.where.not(email: nil)

相关内容

最新更新