2

Bassicaly my problem what to do if i have 3 forms and one submit button.

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 %>
tomekfranek
  • 6,852
  • 8
  • 45
  • 80
  • This may be of help: http://stackoverflow.com/questions/4641565/rails-3-submit-a-form-with-multiple-records – John May 17 '12 at 04:07

1 Answers1

3

I think you should have defined your form using:

<%= 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 %>

This way it will be easier to treat all email address on your controller and you can track those errors to display them afterwards:

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

I didnt test this code. Hope it helps!

Rudy Seidinger
  • 1,059
  • 13
  • 22
  • But ```render :name_of_the_action_that_called_send_invitations ``` is to antoher controller with id – tomekfranek May 17 '12 at 11:56
  • I don't quite understood you, but if you're redirecting user to another controller, not related to the form itself, something feels wrong... There is no new action insde FreeRegistrationCouponsController? – Rudy Seidinger May 17 '12 at 19:00