Ruby on rails 如何使用一个表单创建三个记录?以及如何为它们显示验证错误?
基本上我的问题是,如果我有3个表单和一个提交按钮,该怎么办 我想创建一个表单,向每个收件人发送电子邮件,然后在免费登记优惠券表中创建新记录 我需要验证此表单的电子邮件 免费注册优惠券的型号:收件人\电子邮件、令牌、发件人\ id 现在我有这个:Ruby on rails 如何使用一个表单创建三个记录?以及如何为它们显示验证错误?,ruby-on-rails,ruby,ruby-on-rails-3,forms,Ruby On Rails,Ruby,Ruby On Rails 3,Forms,基本上我的问题是,如果我有3个表单和一个提交按钮,该怎么办 我想创建一个表单,向每个收件人发送电子邮件,然后在免费登记优惠券表中创建新记录 我需要验证此表单的电子邮件 免费注册优惠券的型号:收件人\电子邮件、令牌、发件人\ id 现在我有这个: class FreeRegistrationCouponsController < ApplicationController def send_invitations emails = [params[:recipient_emai
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
class FreeRegistrationCouponsControllere,:发件人\ id=>current\用户.id)
#梅勒
结束
将_重定向到根路径:notice=>“您只需发送#{emails.size}邀请!”
其他的
将_重定向到(:返回)
结束
结束
结束
类免费注册优惠券true,:email=>true
def生成令牌
self.token=SecureRandom.hex
结束
结束
这是另一个控制器车辆控制器#确认中的表单:
<%= 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 %>
“免费注册优惠券”,操作=>“发送邀请”do%>
我认为您应该使用以下方法定义表单:
<%= form_tag :controller => 'free_registration_coupons', :action => "send_invitations" do %>
<%= @error_message %>
<%= 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 %>
“免费注册优惠券”,操作=>“发送邀请”do%>
这样可以更轻松地处理控制器上的所有电子邮件地址,并且您可以跟踪这些错误以在以后显示它们:
class FreeRegistrationCouponsController < ApplicationController
def send_invitations
emails = params[:recipient_email]
emails.reject!{ |param, value| value.eql?("") }
errors = []
if emails.any?
emails.each do |param, value|
validation_result = FreeRegistrationCoupon.save(:recipient_email => value, :sender_id => current_user.id)
#MAILER
end
redirect_to root_path, :notice => "You just send #{emails.size} invitations!"
else
@error_message = "You have to include, at least, one e-mail address!"
render :name_of_the_action_that_called_send_invitations
end
end
end
class FreeRegistrationCouponsController值,:发件人\u id=>当前用户.id)
#梅勒
结束
将_重定向到根路径:notice=>“您只需发送#{emails.size}邀请!”
其他的
@error\u message=“您必须至少包括一个电子邮件地址!”
render:name___称为发送邀请的行动的名称
结束
结束
结束
我没有测试这个代码。希望有帮助 这可能会有所帮助:但是
render:name\u被称为\u send\u investments\u的\u action\u是发送给另一个idI控制器的\u。我不太明白您的意思,但是如果您将用户重定向到另一个控制器,而与表单本身无关,则会感觉出问题。。。DE FreeRegistrationCouponsController中没有新操作?