将simple_form中的输入添加到数组中

时间:2014-09-03 23:28:55

标签: ruby-on-rails ruby arrays simple-form actionmailer

我尝试使用simple_form收集一个或多个电子邮件地址,然后将这些电子邮件地址作为数组传递,以便ActionMailer可以向这些地址发送邀请电子邮件。不确定如何将所有输入字段放入一个传递给控制器​​和邮件程序的数组中。这是我到目前为止所拥有的。

输入表格:

<div class="user-group-partial">
<%= simple_form_for :user_emails, :url => "/user_groups/sent_emails/", :method => :post do |f| %>
    <%= f.error_notification %>
    <div class="form-inputs">
            <%= f.input :email, :maxlength => 25 %>
            <%= f.input :email, :maxlength => 25 %>
            <%= f.input :email, :maxlength => 25 %>
        <br>
        <div class="form-actions">
            <%= f.button :submit, :class => "btn btn-primary", :value => "Invite Bros" %>
        </div>
    </div>
<% end %>
<br>

控制器方法:

def send_invite_to_members   
    token = current_user.user_group.token
    email = params[:user_emails][:email]
    UserGroupMailer.group_invitation_email(email, token).deliver
    redirect_to '/user_groups', :notice => "Your invitations have been sent"
end

ActionMailer方法:

def group_invitation_email(email_address, token)
    @token = token.to_s
    mail to: email_address, subject: "You've been invited!"
end

谢谢!

1 个答案:

答案 0 :(得分:1)

解决此问题的一种简单方法是使用带有帮助程序标签的文本字段,该文本字段指示用户键入以空格或逗号分隔的电子邮件地址。

然后在您的控制器中,您可以拆分它们并发送每封电子邮件,例如:

def send_invite_to_members   
  token = current_user.user_group.token
  emails = params[:user_emails][:emails].split(",") # or split(" ") if you want them separated by a space
  emails.each do |e|
    UserGroupMailer.group_invitation_email(e, token).deliver
  end
  redirect_to '/user_groups', :notice => "Your invitations have been sent"
end