向多个逗号分隔的电子邮件地址发送电子邮件

时间:2021-06-08 20:21:02

Hi I want to send emails to all the email comma addresses entered in text field. I have following controller for doing so.

你好,我想发邮件给所有在文本框中输入的逗号地址。我有跟随控制器这样做。

 def send_invites  #friend invites
     @emails_txt = params[:emails_txt]
     @emails = @emails_txt.split(/\s*,\s*/)
      @ve = []
      @emails.each do |email|
         if email =~ /\b[A-Z0-9._%a-z-]+@(?:[A-Z0-9a-z-]+.)+[A-Za-z]{2,4}\z/
           @ve << email
        end
      end
@all_ok = true
@errors = []

    @ve.each do |email|

        @invitation = Invitation.new
        @invitation.sender = current_user
        @invitation.recipient_email = email

      if @invitation.save
        UserMailer.invitation_notification(current_user, @invitation, new_user_registration_url+"/#{@invitation.token}").deliver
      else          
        @all_ok = "false"
        @errors << @invitation.errors       
      end
   end
end

It was working fine but now it is giving error as follows

它运行得很好,但是现在它给出了如下错误。

NoMethodError in InvitationsController#send_invites 
undefined method `username' for nil:NilClass

whats the problem please help me. Thank you

有什么问题请帮助我。谢谢你!

1 个解决方案

#1


3  

It looks like you are perhaps referencing a nil object in your email view.

看起来您可能正在您的电子邮件视图中引用nil对象。

I would look in your view for the UserMailer#invitation_controller action and see where you reference username.

我将在您的视图中查找UserMailer#invitation_controller操作,并查看您在何处引用用户名。

Then, once you found that, trace back the object and find out why its nil, then add some testing around that (i.e. if current_user.nil? ...).

然后,一旦找到这个对象,跟踪它并找出为什么它是nil,然后围绕它添加一些测试(例如,如果current_user.nil?……)。

#1


3  

It looks like you are perhaps referencing a nil object in your email view.

看起来您可能正在您的电子邮件视图中引用nil对象。

I would look in your view for the UserMailer#invitation_controller action and see where you reference username.

我将在您的视图中查找UserMailer#invitation_controller操作,并查看您在何处引用用户名。

Then, once you found that, trace back the object and find out why its nil, then add some testing around that (i.e. if current_user.nil? ...).

然后,一旦找到这个对象,跟踪它并找出为什么它是nil,然后围绕它添加一些测试(例如,如果current_user.nil?……)。