How to use ruby on rails to construct a scaffold that also contains a different scaffold that can be added to it multiple times?

59 views Asked by At

I'm new to ruby on rails and in the efforts of "not repeating myself" in my code, I am trying to create a scaffold that contains one or more (decided by user) of a different scaffold. But I am lost and Im not sure that it can be done.

Example: 1. There is an "article" scaffold that contains "sections" along with standard information (title, date of article, main image, main topics, etc) 2. The "article" type can has_many "sections". Each section contains an order reference, header text and body text of the section. But the sections are small.
3. In this example, the article will be displayed with it's overview text AND each section that is assigned to it.

I am trying to construct this through ruby but do not know how to architect it. What is the best practice? Creating an article that has many sections built into it (some not used) would be a waste of memory for the article. Creating the "section" data point would allow me to easily create sections, but they would not be visibly connected and the editor would have to do a lot of work to make sure that it makes sense.

The best idea I had, which ruby does not seem to support, is the create an "article" type that has as many "sections" as the editor see's fit. Theoretically this would allow the editor to create a new article and while filling in the article information, be able to write the individual sections one at a time, with the ability to add additional sections on the same screen. But again I am new and not sure if that is even possible...

Thanks

1

There are 1 answers

0
engineersmnky On

Copied from my comment:

This can be done but not as a "scaffold" which is a rails generator for quickly building a controller, model and views in 1 shot. What you are looking for is a rendered partial. Take a look at nested_form Or cocoon gems to get an idea of how to implement these dynamically.