Using nested model forms with polymorphic associations

Hello -

Sorry if this is long - I’m erring on the side of giving too much
information rather than too little. Hopefully it’ll appear organized and
easy to skim.

In a nutshell, I’m making a site where people can put together online
tutorials. Each tutorial is broken up into sections, and there are two
types of sections at the moment: Examples and Questions. I decided to do
this with Polymorphic Associations, which look like this:

class Tutorial < ActiveRecord::Base
has_many :sections, :order => ‘sections.position ASC’
accepts_nested_attributes_for :sections, :allow_destroy => true

class Section < ActiveRecord::Base
belongs_to :tutorial
belongs_to :sectionable, :polymorphic => true
accepts_nested_attributes_for :sectionable, :allow_destroy => true

class Example < ActiveRecord::Base
has_one :section, :as => :sectionable

class Question < ActiveRecord::Base
has_one :section, :as => :sectionable

Defining a Section model this way has been very useful - it has a
position attribute, so I can easily keep examples and questions ordered
correctly, and it also has an “anchor” attribute, so that the user can
define named anchors for the sections of each tutorial (i.e. “houses”
for /tutorials/architecture#houses)

Now, I’m trying to set up a nested model form, so that when a tutorial
is being edited, all the examples and questions are also presented (in
the order they appear in the tutorial) and can be edited there, too.

The section portion of the edit tutorial page looks like:

<% for section in @sections %>
<% f.fields_for section do |g| %>
<%= g.label :anchor %><br >
<%= g.text_field :anchor %>

<% if section.sectionable_type == "Example" %>

  <% g.fields_for :sectionable do |h| %>
    <%= h.label :title %><br \>
    <%= h.text_field :title %>

    <%= h.label :content %><br \>
    <%= h.text_area :content %>
  <% end %>

<% elsif section.sectionable_type == "Question" %>

  <% g.fields_for :sectionable do |h| %>
    <%= h.label :title %><br \>
    <%= h.text_field :title %>

    <%= h.label :prompt %><br \>
    <%= h.text_area :prompt %>

    <%= h.label :answer %><br \>
    <%= h.text_area :answer %>
  <% end %>
<% end %>

<% end %>
<% end %>

As far as I can tell, this is how the nested models are supposed to
look, but this isn’t working. When I load up the edit tutorial page
(I’ve already populated the database with with Lorem Ipsum-type stuff)
it appears correctly - each example and question has their own edit box
with the current information already filled in. But when I try to submit
the form, I get:

unknown attribute: section

Previously, while tweaking, I’ve also gotten unknown attribute: example
and unknown attribute: question. Looking at the source of the edit
tutorial page, one of the spots where a question can be defined looks
like:

Anchor<br >

Title<br >

Prompt<br
>
Autem et labore
voluptas…

Answer<br
>
Qui consectetur
beatae…

In other words, it doesn’t look like there’s anything in the html to
differentiate the contents of one question from another - when the form
is processed, the information from each question overwrites that from
the previous question, and the final params hash is just a jumble of
incomplete information.

I’m not sure how to fix this, or if nested model forms can even be used
with polymorphic associations in this manner. Any ideas?

Thanks!
Chris