【发布时间】:2015-11-05 19:48:52
【问题描述】:
我有一个与每个记分牌相关的邀请功能。每个记分牌都有_许多已发送的_邀请,并且每个邀请都属于_一个记分牌。该功能运行良好。当前界面允许管理员分别输入每个电子邮件地址以发送邀请。
但是,我想改进界面,允许管理员以相同的格式输入多个电子邮件地址,用逗号分隔或突出显示以让用户知道电子邮件已完成,然后发送一封电子邮件至所有这些电子邮件地址。
我不确定如何让我的表单接受以逗号或任何其他方式分隔的多封电子邮件?我不认为 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
【问题讨论】:
标签: ruby-on-rails email