Jak stworzyć 3 rekordy używając jednego formularza?

Cześć,
chcę stworzyć formularz, który wyśle trzy emaile oraz stworzy trzy obiekty w modelu FreeRegistrationCoupon.

Model FreeRegistrationCoupon: recipient_email, token, sender_id

[code=ruby] 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[/code]

Formularz znajduje sie w widoku innego kontrollera po wykonaniu akcji confirm CarsController#confirm:
Chcę też aby wyświetlaly sie bledy walidacji.

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

Rayan -> Screencasty -> Te o formularzach -> complex cos tam

@gotar: http://railscasts.com/episodes/73-complex-forms-part-1 chyba ten miałeś na myśli, zaraz go odpale, dzięki

Albo tak, tylko mam problem z tym jak ten formularz i renderowanie widoku w przypadku gdy @registration.valid? jest false jeżeli mój formularz pojawia się nie w ## app/views/registrations/new.html.erb tylko po pomyślnym zarejestrowaniu innego modelu ## app/views/cars/confirm.html.erb
hmm, może redirect_to(:back) uzyć?

[code=ruby]## app/controllers/registrations_controller.rb

class RegistrationsController < ApplicationController
def new
@registration = Registration.new
end

def create
@registration = Registration.new params[:registration]
if @registration.save
# yay
else
render :action => ‘new’
end
end
end

app/views/registrations/new.html.erb

Yay, creating registration!

<%= form_for @registration do |f| %>
<%= f.text_field :email1 %>
<%= f.text_field :email2 %>
<%= f.text_field :email3 %>
<%= f.submit ‘Register!’ %>
<% end -%>

app/models/registration.rb

This is the hard part

class Registration
def initialize(params)
@emails = params.values_at :email1, :email2, :email3
end

def save
emails.reject!{ |e| e.eql?("") }

if emails.any?
  emails.each do |e|
    FreeRegistrationCoupon.create(:recipient_email => e, :sender_id => current_user.id)
    #MAILER
  end
  true
else
  false
end

end
end[/code]