Nested forms not rendering with polymorphic models #966

Closed
seanknox opened this Issue Aug 31, 2013 · 6 comments

Comments

Projects
None yet
3 participants

Hi, I have a nested form per the example in the README. My form uses the Person model, which accepts_nested_attributes_for the Address model. When using accepts_nested_attributes_for :address however, none of the fields for the nested form are shown. I'm wondering if the issue is because Address belongs_to :addressable, :polymorphic => true ?

Here's my form:

= semantic_form_for @person, url: new_users_path, html: { class: "form-horizontal"} do |f|
    = f.semantic_errors
    = f.inputs do
      = f.input :first_name, :required => false
      = f.input :last_name, :required => false
      = f.input :mobile, :required => false
      = f.input :alternate_phone, :required => false
      = f.semantic_fields_for :address do |address|
        = address.input :street1, :required => false
        = address.input :street2, :required => false
        = address.input :city, :required => false
    = f.actions do
      = f.action :submit, :as => :button, :button_html => {:class => "btn btn-primary"}

Here's the corresponding models:

class Person < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :mobile, :alternate_phone
  has_one :address, :as => :addressable
  accepts_nested_attributes_for :address
end

class Address < ActiveRecord::Base
  attr_accessible :city, :street1, :street2
  belongs_to :addressable, :polymorphic => true
end

I had a similar problem and I think this is still an issue. I wrote a stack overflow about it over a month ago and have not yet found a solution. In short, it seems the polymorphic related table is unable to populate through formtastic. Providing link to SO below:

http://stackoverflow.com/questions/19500053/rails-formtastic-polymorphic-relationships

Was this issue ever addressed? I still have no found a way around this.

Owner

justinfrench commented Mar 31, 2014

@seanknox @busboybud I'd love to help, but you're making it hard for me.

  1. I need to know version numbers (Rails, Formtastic, anything else that might be relevant)
  2. If you're on older versions of either, is it possible to upgrade?
  3. Could you try bundling in edge/master of Formtastic to verify that the fix isn't sitting unreleased
  4. Is it possible to build a form for this relationship using standard Rails helpers (proving that it's a bug in Formtastic, not your modelling & associations)?
  5. Super mega bonus points by creating/sharing a small-as-possible test application that illustrates the bug so that I can use this as a reference for finding a solution.

Failing all of that, I'm not a fan of accepts_nested_attributes_for. The work-around (especially if it might take us a long time to resolved this) is pretty big, but I'd highly recommend it anyway:

Build a presenter-style ActiveModel that presents the attributes for the two models (Person, Address) as one unified model (e.g. Contact) with a flat set of attributes (e.g. @contact.address_street1), then handle the delegation to the ActiveRecord models and associations yourself. I'm not aware of a great tutorial on this to link to sorry.

The result would be a form simplified to:

= semantic_form_for @contact, url: new_users_path, html: { class: "form-horizontal"} do |f|
    = f.semantic_errors
    = f.inputs do
      = f.input :first_name, :required => false
      = f.input :last_name, :required => false
      = f.input :mobile, :required => false
      = f.input :alternate_phone, :required => false
      = f.input :address_street1, :required => false
      = f.input :address_street2, :required => false
      = f.input :address_city, :required => false
    = f.actions do
      = f.action :submit, :as => :button, :button_html => {:class => "btn btn-primary"}

Anyway, if there's a bug, help me help fix it. If you're interested in these wrapper-style ActiveModel objects to present multiple models as one, go do some googling!

Leaving open for now, will close after a month or so, but I'd love your help to narrow down where the bug might be through the above steps.

Owner

justinfrench commented Mar 31, 2014

I also started looking through our documentation for accepts_nested_attributes_for and noticed that neither of you are strictly following either convention.

The first follows the fields_for pattern, and I think it requires you to have a nested inputs block (which you don't in the example above nor on the SO link):

<% semantic_form_for @user do |form| %>
  <%= f.inputs :name, :email %>
  <% f.semantic_fields_for :profile do |profile| %>
    <% profile.inputs do %>
      <%= profile.input :biography %>
      <%= profile.input :twitter_name %>
    <% end %>
  <% end %>
<% end %>

The second reduces the lines by combining the fields_for and inputs methods into one:

<% semantic_form_for @user do |f| %>
  <%= f.inputs :name, :email %>

  <% f.inputs :for => :profile do %>
    <%= profile.input :biography %>
    <%= profile.input :twitter_name %>
    <%= profile.input :shoe_size %>
  <% end %>
<% end %>

Maybe this is not the cause of the bug, but it'd be worth trying both of these patterns instead of what you both have. Even if only to to rule it out and keep us focused on finding a solution.

Owner

justinfrench commented Apr 19, 2014

I don't have enough information to work on this. Closing for now, but would love to re-open with new information and fix this.

Hi justin, sorry for the delayed response. I'd love to pursue this bug further, but current life events have been absorbing time usually spent on coding projects. When I free up, I will take a look at your suggestions and hopefully we can re-engage this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment