Daniel Bogart
Daniel Bogart

Reputation: 1295

Adding inputs from simple_form into array

I'm trying to use simple_form to gather one or multiple email address, then pass those email addresses as an array so that ActionMailer can send out invitation emails to those addresses. Unsure about how to get all the input fields into one array that is passed to the controller and mailer. Here is what I have so far.

Input form:

<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>

Controller Method:

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 Method:

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

Thanks!

Upvotes: 1

Views: 2293

Answers (1)

Josh
Josh

Reputation: 5721

One simple way to solve this is to use a text field with a helper label that instructs the user to type email addresses separated by spaces or commas.

Then in your controller you can split them up and send out each email with something like:

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

Upvotes: 1

Related Questions