0

I'm trying to build a model which render/process partials from others models. Basically, a master model would provide some generic features while embedding different models depending on user input.

In other words, within the same model, I need to render "_form.html.erb" from various models and save them as a nested model would do.

I know nested model would do the work if it was always the same type of model.

class Master < ActiveRecord::Base
  has_one :{change depending on user input}
  accepts_nested_attributes_for :{change depending on user input}
end

I seen polymorphic models but it seems to do the exact opposite (i.e. one partial in multiple models).

Any ideas? Polymorphic nested model or something?

northox
  • 432
  • 5
  • 18

1 Answers1

1

It definitely looks like you need a polymorphic association here, but accepts_nested_attributes_for does not support polymorphism.

However, take a look at this related question accepts_nested_attributes_for with belongs_to polymorphic

Maybe in your form you could do something like the following to render the correct _form partial:

<%= render :partial => File.join(@master.thing.class.name.underscore.pluralize, "form") %>

Good luck!

Community
  • 1
  • 1
Wizard of Ogz
  • 12,543
  • 2
  • 41
  • 43