Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion How to create three objects/records using one form? And how to show validation errors for them?

Received: by 10.52.35.112 with SMTP id g16mr1646066vdj.4.1336068939385;
        Thu, 03 May 2012 11:15:39 -0700 (PDT)
X-BeenThere: rubyonrails-talk@googlegroups.com
Received: by 10.52.109.106 with SMTP id hr10ls141482vdb.0.gmail; Thu, 03 May
 2012 11:15:12 -0700 (PDT)
Received: by 10.52.25.79 with SMTP id a15mr60112vdg.10.1336068912914;
        Thu, 03 May 2012 11:15:12 -0700 (PDT)
Date: Thu, 3 May 2012 11:15:12 -0700 (PDT)
From: regedarek <dariusz.fins...@gmail.com>
To: rubyonrails-talk@googlegroups.com
Message-ID: <22314306.724.1336068912409.JavaMail.geo-discussion-forums@vbuo17>
Subject: How to create three objects/records using one form? And how to show
 validation errors for them?
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_722_9923216.1336068912402"

------=_Part_722_9923216.1336068912402
Content-Type: multipart/alternative; 
	boundary="----=_Part_723_19127373.1336068912408"

------=_Part_723_19127373.1336068912408
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit



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


------=_Part_723_19127373.1336068912408
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<p style=3D"font-size: 14px; background-image: initial; background-attachme=
nt: initial; background-origin: initial; background-clip: initial; clear: b=
oth; word-wrap: break-word; color: rgb(0, 0, 0); font-family: Arial, 'Liber=
ation Sans', 'DejaVu Sans', sans-serif; text-align: left; ">I want to creat=
e a form which sends email to each recipient and then create new record in =
free_registration_coupons table.</p><p style=3D"font-size: 14px; background=
-image: initial; background-attachment: initial; background-origin: initial=
; background-clip: initial; clear: both; word-wrap: break-word; color: rgb(=
0, 0, 0); font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;=
 text-align: left; ">I need validation of email for this form.</p><p style=
=3D"font-size: 14px; background-image: initial; background-attachment: init=
ial; background-origin: initial; background-clip: initial; clear: both; wor=
d-wrap: break-word; color: rgb(0, 0, 0); font-family: Arial, 'Liberation Sa=
ns', 'DejaVu Sans', sans-serif; text-align: left; "><img src=3D"http://i.st=
ack.imgur.com/YZBly.png" alt=3D"enter image description here" title=3D"" st=
yle=3D"background-image: initial; background-attachment: initial; backgroun=
d-origin: initial; background-clip: initial; background-color: transparent;=
 max-width: 640px; "></p><p style=3D"font-size: 14px; background-image: ini=
tial; background-attachment: initial; background-origin: initial; backgroun=
d-clip: initial; clear: both; word-wrap: break-word; color: rgb(0, 0, 0); f=
ont-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align=
: left; ">Model&nbsp;<strong style=3D"background-image: initial; background=
-attachment: initial; background-origin: initial; background-clip: initial;=
 background-color: transparent; ">FreeRegistrationCoupon</strong>: recipien=
t_email, token, sender_id</p><p style=3D"font-size: 14px; background-image:=
 initial; background-attachment: initial; background-origin: initial; backg=
round-clip: initial; clear: both; word-wrap: break-word; color: rgb(0, 0, 0=
); font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-a=
lign: left; ">For now i have this:</p><pre style=3D"margin-bottom: 10px; pa=
dding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px;=
 font-size: 14px; background-image: initial; background-attachment: initial=
; background-origin: initial; background-clip: initial; background-color: r=
gb(238, 238, 238); font-family: Consolas, Menlo, Monaco, 'Lucida Console', =
'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier=
 New', monospace, serif; overflow-x: auto; overflow-y: auto; width: auto; m=
ax-height: 600px; color: rgb(0, 0, 0); line-height: 18px; text-align: left;=
 "><code style=3D"background-image: initial; background-attachment: initial=
; background-origin: initial; background-clip: initial; font-family: Consol=
as, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono',=
 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; ">class FreeR=
egistrationCouponsController &lt; ApplicationController
  def send_invitations
    emails =3D [params[:recipient_email_1], params[:recipient_email_2], par=
ams[:recipient_email_3]]
    emails.reject!{ |e| e.eql?("") }

    if emails.present?
      emails.each do |e|
        FreeRegistrationCoupon.create(:recipient_email =3D&gt; e, :sender_i=
d =3D&gt; current_user.id)
        #MAILER
      end
      redirect_to root_path, :notice =3D&gt; "You just send #{emails.size} =
invitations!"
    else
      redirect_to(:back)
    end
  end
end


class FreeRegistrationCoupon &lt; ActiveRecord::Base
  before_save :generate_token

  attr_accessor :recipient_email, :sender_id
  validates :recipient_email, :presence =3D&gt; true, :email =3D&gt; true

  def generate_token
    self.token =3D SecureRandom.hex
  end
end
</code></pre><p style=3D"font-size: 14px; background-image: initial; backgr=
ound-attachment: initial; background-origin: initial; background-clip: init=
ial; clear: both; word-wrap: break-word; color: rgb(0, 0, 0); font-family: =
Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; ">Th=
is is form which is in other controller&nbsp;<strong style=3D"background-im=
age: initial; background-attachment: initial; background-origin: initial; b=
ackground-clip: initial; background-color: transparent; ">CarsController#co=
nfirm</strong>:</p><pre style=3D"margin-bottom: 10px; padding-top: 5px; pad=
ding-right: 5px; padding-bottom: 5px; padding-left: 5px; font-size: 14px; b=
ackground-image: initial; background-attachment: initial; background-origin=
: initial; background-clip: initial; background-color: rgb(238, 238, 238); =
font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', =
'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, s=
erif; overflow-x: auto; overflow-y: auto; width: auto; max-height: 600px; c=
olor: rgb(0, 0, 0); line-height: 18px; text-align: left; "><code style=3D"b=
ackground-image: initial; background-attachment: initial; background-origin=
: initial; background-clip: initial; font-family: Consolas, Menlo, Monaco, =
'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sa=
ns Mono', 'Courier New', monospace, serif; ">&lt;%=3D form_tag :controller =
=3D&gt; 'free_registration_coupons', :action =3D&gt; "send_invitations" do =
%&gt;
  &lt;!-- errors --&gt;
  &lt;%=3D label_tag :recipient_email_1 %&gt;
  &lt;%=3D text_field_tag :recipient_email_1 %&gt;
  &lt;%=3D label_tag :recipient_email_2 %&gt;
  &lt;%=3D text_field_tag :recipient_email_2 %&gt;
  &lt;%=3D label_tag :recipient_email_3 %&gt;
  &lt;%=3D text_field_tag :recipient_email_3 %&gt;
  &lt;%=3D submit_tag %&gt;
&lt;% end %&gt;</code></pre>
------=_Part_723_19127373.1336068912408--

------=_Part_722_9923216.1336068912402--