提取未通过验证的对象并在通知中显示它们

时间:2013-02-22 13:02:13

标签: ruby ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

简介简而言之,此操作让您通过邮件邀请更多用户,您可以在其中放置电子邮件并提交电子邮件,并通过邮件收到电子邮件。

模型中有一个方法可以验证project_invitation创建的电子邮件,因此插入无效的电子邮件类型(如:eee @ em,fff.com,bbb @ .com),pinv将不被保存,logger.error将被提升..

问题:如何提取所有未通过验证的电子邮件并将其显示在通知中,例如“邀请已发送,eee @ em,fff.com ,bbb @ .com,电子邮件无效“

谢谢,如果需要任何其他信息,请告诉我。

  def invite_users
    emails = params[:project_invitation][:emails]
    role = current_user.has_role?(:admin, @project) ? :admin : :default

    emails.to_s.split(',').each do |email|
      pinv = ProjectInvitation.new(params[:project_invitation], as: role)
      pinv.sender = current_user
      pinv.recipient_email = email
      pinv.recipient = nil
      pinv.project = @project

      if pinv.save
        ProjectMailer.delay.invitation(pinv)
      else
        logger.error("Failed to save project inv: #{pinv.errors.full_messages.join("\n")}")
      end
    end
  end

1 个答案:

答案 0 :(得分:0)

在您的logger.error

上方捕获您阻止的电子邮件
  invalid_emails = []

  if pinv.save
    ProjectMailer.delay.invitation(pinv)
  else
    invalid_emails << email
    logger.error("Failed to save project inv: #{pinv.errors.full_messages.join("\n")}")
  end

  if invalid_emails.any?
    flash[:notice] = "Invalid emails: #{invalid_emails.join(', ')}"
  end

整个代码:

  def invite_users
    emails = params[:project_invitation][:emails]
    role = current_user.has_role?(:admin, @project) ? :admin : :default

    invalid_emails = []

    emails.to_s.split(',').each do |email|
      pinv = ProjectInvitation.new(params[:project_invitation], as: role)
      pinv.sender = current_user
      pinv.recipient_email = email
      pinv.recipient = nil
      pinv.project = @project

      if pinv.save
        ProjectMailer.delay.invitation(pinv)
      else
        invalid_emails << email
        logger.error("Failed to save project inv: #{pinv.errors.full_messages.join("\n")}")
      end
    end

    if invalid_emails.any?
      flash[:notice] = "Invalid emails: #{invalid_emails.join(', ')}"
    end
  end