2 levels nested form with a polymorphic association

Hi,

I’m trying to build a complex nested form for the model ‘page’, which
has a polymorphic association with different component types.
Unfortunately it does not seem to work properly.

model “Page” :

has_many :components, :dependent => :destroy
has_many :textcomponents, :through => :components, :source =>
:componentobject , :source_type => “Textcomponent”
has_many :componentobjects,:through =>:components
accepts_nested_attributes_for :components, :allow_destroy => true ,
:reject_if=>:all_blank

the polymorphic middle model “Component” :

belongs_to :componentobject, :polymorphic => true
belongs_to :page
accepts_nested_attributes_for :componentobject

a specific component model on the other end ‘Textcomponent’ :

has_one :component, :as => :componentobject
has_one :page, :through => :component

In the ‘form’ partial of the page controller, I have this code:

<%= form_for(@page) do |f| %>
<% f.fields_for :components do |cmp_form| %>
<%= cmp_form.label :cid %>
<%= cmp_form.text_field :cid %>

<% cmp_form.fields_for :componentobject do |cmpo_form| %>
<% if cmpo_form.object.class == Textcomponent %>
<%= cmpo_form.label :value_nl %>
<%= cmpo_form.text_field :value_nl %>
<% end %>
<% end %>
<% end %>
<% end %>

And of course in the “Pages” controller I create the nested objects :

def new
@page = Page.new
@c = @page.components.build
@c.componentobject = Textcomponent.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @page }
end

end

In this case I assume that my ‘cmpo_form’ part of the form should render
one instance of the the “Textcomponent”. Unfortunately it doesn’t… I
looks
like rails is not finding any textcomponents in the component instance
of the page.

I’m not sure if I’m missing something, or it just isn’t possible or good
practice.

Any suggestions would be welcome.