I want to create a form which sends email to each recipient and then create 
new record in free_registration_coupons table.

I need validation of email for this form.

[image: enter image description here]

Model *FreeRegistrationCoupon*: recipient_email, token, sender_id

For now i have this:

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

This is form which is in other controller *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 %>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-talk/-/ArDG_772-NgJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to