Chciałbym dodać do Usera zarządzanie jego emailami. (Każdy user może mieć email domowy, z pracy, inny) oraz jeden z tych emaili powinien być oznaczony jako główny.
Mamy zatem taki układ modeli:
[code=ruby]class Profile < ActiveRecord::Base
attr_accessible :available_at, :person_id, :person_attributes
belongs_to :person
accepts_nested_attributes_for :person
delegate :first_name, :last_name, to: :person
end
class Person < ActiveRecord::Base
attr_accessible :title, :emails_attributes
has_one :profile
has_many :emails, class_name: ‘ContactType::Email’
accepts_nested_attributes_for :emails, allow_destroy: true
end
class ContactType::Email < ActiveRecord::Base
EMAIL_TYPE = { work: 0, private: 1, other: 2 }
attr_accessible :email_type, :address, :person_id
belongs_to :person
def email_type
EMAIL_TYPE.key(read_attribute(:email_type))
end
def email_type=(t)
write_attribute(:email_type, EMAIL_TYPE[t.to_sym])
end
end[/code]
W jaki sposób uzyskalibyście możliwość dodawania emaili z poziomu formularza profiles/_form.html ?
= simple_form_for(@profile) do |form|
= render 'person_form', f: form
= render 'profile_form', f: form
= render 'contact_type_form', f: form
= form.button :submit
Jest tutaj jakby podwójne zagnieżdżenie. Próbuję to zrobić z dedykowanym Railscastem ale bez sukcesów.