How to create three objects/records using one form? And how to show validation errors for them?

17 views
Skip to first unread message

regedarek

unread,
May 3, 2012, 2:15:12 PM5/3/12
to rubyonra...@googlegroups.com

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.

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 %>
Reply all
Reply to author
Forward
0 new messages