【发布时间】:2025-12-28 07:25:11
【问题描述】:
如果我有 3 个表单和一个提交按钮,我的问题基本上是什么。
我想创建一个向每个收件人发送电子邮件的表单,然后在 free_registration_coupons 表中创建新记录。
我需要验证此表单的电子邮件。
模型 FreeRegistrationCoupon:recipient_email、token、sender_id
现在我有这个:
class FreeRegistrationCouponsController < ApplicationController
def send_invitations
emails = [params[:recipient_email_1], params[:recipient_email_2], params[:recipient_email_3]]
emails.reject!{ |e| e.eql?("") }
if emails.present?
emails.each do |e|
FreeRegistrationCoupon.create(:recipient_email => e, :sender_id => current_user.id)
#MAILER
end
redirect_to root_path, :notice => "You just send #{emails.size} invitations!"
else
redirect_to(:back)
end
end
end
class FreeRegistrationCoupon < ActiveRecord::Base
before_save :generate_token
attr_accessor :recipient_email, :sender_id
validates :recipient_email, :presence => true, :email => true
def generate_token
self.token = SecureRandom.hex
end
end
这是其他控制器中的表单CarsController#confirm:
<%= form_tag :controller => 'free_registration_coupons', :action => "send_invitations" do %>
<!-- errors -->
<%= label_tag :recipient_email_1 %>
<%= text_field_tag :recipient_email_1 %>
<%= label_tag :recipient_email_2 %>
<%= text_field_tag :recipient_email_2 %>
<%= label_tag :recipient_email_3 %>
<%= text_field_tag :recipient_email_3 %>
<%= submit_tag %>
<% end %>
【问题讨论】:
-
这可能会有所帮助:*.com/questions/4641565/…
标签: ruby-on-rails ruby ruby-on-rails-3 forms