如何使用相同的表单字段向多个收件人发送电子邮件

时间:2015-11-05 19:48:53

标签: ruby-on-rails email

我有与每个记分板相关联的邀请功能。每个记分板has_many sent_invitations,每个邀请都属于记分板。该功能完美无缺。当前界面允许管理员分别输入每个电子邮件地址以发送邀请。

但是,我希望改进界面,允许管理员输入以逗号分隔的相同表单的多个电子邮件地址,或以某种方式突出显示电子邮件已完成,然后发送一封电子邮件至这些电子邮件地址。

我不知道如何让我的表单接受以逗号或其他方式分隔的多封电子邮件?我不认为rails为此提供了不同的表单助手。我如何在我的控制器中处理它?我该如何为此设置正则表达式?我从未实现过接受多个电子邮件地址的表单,然后向每个电子邮件地址发送一封电子邮件。有关该主题的任何信息都非常有用。

新表格的代码。

<h1>Invitation</h1>
<div class="row">
    <div class= "col-md-4 col-md-offset-4">
      <%= form_for [@scoreboard, @invitation] do |f| %> <!-- you have to pass in the scoreboard id into the forms as well -->
        <%= render 'shared/error_messages', object: f.object %>

        <%= f.label :recipient_name %>
        <%= f.text_field :recipient_name, class: "form-control", placeholder: "First and last name." %>

        <%= f.label :recipient_email, placeholder: "Enter email" %>
        <%= f.text_field :recipient_email, class: "form-control", placeholder: "Enter a valid email address." %>

        <%= f.submit "Send Invitation", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

邀请控制器代码如下:

def create
        @scoreboard = Scoreboard.find(params[:scoreboard_id])
        @invitation = @scoreboard.sent_invitations.build(invitation_params)
        if @invitation.save && User.exists?(email: @invitation.recipient_email) == true
            flash[:success] = "Invitation sent successfully"
            UserMailer.registered_invitation_email(@scoreboard, @invitation_email).deliver_now
            redirect_to new_scoreboard_invitation_path
         elsif
             @invitation.save && User.exists?(email: @invitation.recipient_email) == false
             UserMailer.non_registered_invitation_email(@scoreboard, @invitation).deliver_now
             flash[:success] = "Invitation sent successfully"
             redirect_to new_scoreboard_invitation_path
        else
            render 'new'
        end
    end
end

1 个答案:

答案 0 :(得分:0)

我会写另一种方法来分割电子邮件参数,例如import java.util.Random; import org.junit.Assert; import org.junit.Test; /** * A test for math utils */ public class MathUtilsTest { /** the constructor */ public MathUtilsTest() { super(); } /** test step difference between two values */ @Test(timeout = 3600000) public void testDifferenceBetweenTwoValues() { final Random random; double start, end; int starts, iteration; random = new Random(); for (starts = 333; (--starts) >= 0;) { end = start = -(1d / Math.log(1d - random.nextDouble())); for (iteration = 0; iteration < 3333; iteration++) { Assert.assertEquals(iteration, MathUtils.difference(start, end)); Assert.assertEquals(iteration, MathUtils.difference(end, start)); end = Math.nextUp(end); } } } /** * test the "step" difference of two values, one of which is negative, * the other one being positive */ @Test(timeout = 3600000) public void testDifferenceBetweenTwoValuesOfDifferentSign() { double start, end; int iteration; end = start = 0d; for (iteration = 0; iteration < 333333; iteration++) { Assert.assertEquals( (MathUtils.difference(start, 0d) + // MathUtils.difference(0d, end)), MathUtils.difference(start, end)); Assert.assertEquals( (MathUtils.difference(start, 0d) + // MathUtils.difference(0d, end)), MathUtils.difference(end, start)); start = Math.nextAfter(start, Double.NEGATIVE_INFINITY); end = Math.nextUp(end); } } /** test the border cases of the step difference */ @Test(timeout = 3600000) public void testDifferenceBetweenTwoValuesBorderCases() { Assert.assertEquals(0L, MathUtils.difference(0d, 0d)); Assert.assertEquals(0L, MathUtils.difference(0d, -0d)); Assert.assertEquals(0L, MathUtils.difference(-0d, 0d)); Assert.assertEquals(0L, MathUtils.difference(-0d, -0d)); Assert.assertEquals(1L, MathUtils.difference(0d, Double.MIN_VALUE)); Assert.assertEquals(1L, MathUtils.difference(Double.MIN_VALUE, 0d)); Assert.assertEquals(1L, MathUtils.difference(-0d, Double.MIN_VALUE)); Assert.assertEquals(1L, MathUtils.difference(Double.MIN_VALUE, -0d)); Assert.assertEquals(1L, MathUtils.difference(0d, -Double.MIN_VALUE)); Assert.assertEquals(1L, MathUtils.difference(-Double.MIN_VALUE, 0d)); Assert.assertEquals(1L, MathUtils.difference(-0d, -Double.MIN_VALUE)); Assert.assertEquals(1L, MathUtils.difference(-Double.MIN_VALUE, -0d)); Assert.assertEquals(2L, MathUtils.difference(Double.MIN_VALUE, -Double.MIN_VALUE)); Assert.assertEquals(2L, MathUtils.difference(-Double.MIN_VALUE, Double.MIN_VALUE)); Assert.assertEquals((1L << 52L), MathUtils.difference(0d, Double.MIN_NORMAL)); Assert.assertEquals((1L << 52L), MathUtils.difference(Double.MIN_NORMAL, 0d)); Assert.assertEquals((1L << 52L), MathUtils.difference(-0d, Double.MIN_NORMAL)); Assert.assertEquals((1L << 52L), MathUtils.difference(Double.MIN_NORMAL, -0d)); Assert.assertEquals((1L << 52L), MathUtils.difference(0d, -Double.MIN_NORMAL)); Assert.assertEquals((1L << 52L), MathUtils.difference(-Double.MIN_NORMAL, 0d)); Assert.assertEquals((1L << 52L), MathUtils.difference(-0d, -Double.MIN_NORMAL)); Assert.assertEquals((1L << 52L), MathUtils.difference(-Double.MIN_NORMAL, -0d)); Assert.assertEquals((2L << 52L), MathUtils.difference(Double.MIN_NORMAL, -Double.MIN_NORMAL)); Assert.assertEquals((2L << 52L), MathUtils.difference(-Double.MIN_NORMAL, Double.MIN_NORMAL)); Assert.assertEquals(0L, MathUtils.difference(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)); Assert.assertEquals(0L, MathUtils.difference(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(Double.NaN, Double.NaN)); Assert.assertEquals(-1L, MathUtils.difference(Double.POSITIVE_INFINITY, Double.NaN)); Assert.assertEquals(-1L, MathUtils.difference(Double.NEGATIVE_INFINITY, Double.NaN)); Assert.assertEquals(-1L, MathUtils.difference(Double.NaN, Double.POSITIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(Double.NaN, Double.NEGATIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(0d, Double.NEGATIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(0d, Double.POSITIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(0d, Double.NaN)); Assert.assertEquals(-1L, MathUtils.difference(Double.POSITIVE_INFINITY, 0d)); Assert.assertEquals(-1L, MathUtils.difference(Double.NEGATIVE_INFINITY, 0d)); Assert.assertEquals(-1L, MathUtils.difference(Double.NaN, 0d)); Assert.assertEquals(-1L, MathUtils.difference(1d, Double.NEGATIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(1d, Double.POSITIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(1d, Double.NaN)); Assert.assertEquals(-1L, MathUtils.difference(Double.POSITIVE_INFINITY, 1d)); Assert.assertEquals(-1L, MathUtils.difference(Double.NEGATIVE_INFINITY, 1d)); Assert.assertEquals(-1L, MathUtils.difference(Double.NaN, 1d)); Assert.assertEquals(-1L, MathUtils.difference(-1d, Double.NEGATIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(-1d, Double.POSITIVE_INFINITY)); Assert.assertEquals(-1L, MathUtils.difference(-1d, Double.NaN)); Assert.assertEquals(-1L, MathUtils.difference(Double.POSITIVE_INFINITY, -1d)); Assert.assertEquals(-1L, MathUtils.difference(Double.NEGATIVE_INFINITY, -1d)); Assert.assertEquals(-1L, MathUtils.difference(Double.NaN, -1d)); } } 。这将创建一个由逗号分隔的所有电子邮件的数组。然后做: def comparison_factory(compare_to): def compare_func(x): res = x < compare_to print("is {} less than {}? {}".format(x, compare_to, res)) return res return compare_func funcs = [ comparison_factory(n) for n in range(1,5) ] for f in funcs: f(3) 或类似的东西。

相关问题