Forum: Ruby on Rails Using nested model forms with polymorphic associations

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
710eb9516d466362bc80a2f8d3c31cab?d=identicon&s=25 Chris Hanks (preciousbodilyfluids)
on 2009-05-01 19:07
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

<label for="tutorial_section_anchor">Anchor</label><br \>
<input id="tutorial_section_anchor" name="tutorial[section][anchor]"
size="30" type="text" value="example-sunt" />

<input id="tutorial_section_sectionable_attributes_id"
name="tutorial[section][sectionable_attributes][id]" type="hidden"
value="2" />

for="tutorial_section_sectionable_attributes_title">Title</label><br \>
<input id="tutorial_section_sectionable_attributes_title"
name="tutorial[section][sectionable_attributes][title]" size="30"
type="text" value="Example: Rem Soluta Quidem" />

<textarea id="tutorial_section_sectionable_attributes_prompt"
name="tutorial[section][sectionable_attributes][prompt]">Autem et labore

<textarea id="tutorial_section_sectionable_attributes_answer"
name="tutorial[section][sectionable_attributes][answer]">Qui consectetur

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?

This topic is locked and can not be replied to.