Using accepts_nested_attributes_for with unidirectional table overlay

I have a Post model that belongs_to one Section . There are two different subclasses of Section , and I use STI to implement different behaviors for each of them. In the Post form, I would like to have a tab for each Section . The tab allows the user to either A) select from an existing Section using <select> or B). Let the user create a new Section . I would like to know how to use accepts_nested_attributes_for and fields_for or everything that is required to complete this action of the Rails Path .

Any advice is appreciated. Thanks.

+4
source share
1 answer

Assuming tabs correspond to two subclasses

 class Post # the two subclasses. Each instance will only be using one or the other belongs_to :section_foo belongs_to :section_bar accepts_nested_attributes_for :section_foo accepts_nested_attributes_for :section_bar end 

And in the view (maybe once per tab)

 = form_for @post do |f| = f.select :section_id, SectionFoo.all # etc = fields_for @post.build_section_foo do |s| = s.text_field :bla_bla_bla 

This should give you 85% of the way. You may need to: reject_if bidness on accepts_ * to avoid creating a new section and assigning the old section.

0
source

All Articles