Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing a model of a model in rails
    text
    copied!<p>I am just starting out with rails and have run into a little issue. What I am trying to implement is a way to allow users to create "pages" on which they can tack on widgets.</p> <p>So I have set up a model for "pages", which has database fields such as name, description etc. It also has a "has_many :widgets" association.</p> <p>Here is where I am stumped. I have a list of widgets users can use. Things such as "about me", "photo gallery", "comments", "videos", etc. They can chose one of these, customize the contents and add to their page.</p> <p>i figure, when the user picks and customizes a widget I will add an entry into the widgets table with "belongs_to user_id". However, I need to specify which widget the user chose and the custom contents entered.</p> <p>How do I implement this in Rails? Where can I define each widget type? </p> <p>I can think of a few ways, but not sure which one is the best.</p> <ul> <li>One option is to use a model per widget. I would have AboutMeWidget, VideosWidget, etc. This will allow me to customize each model with whatever fields the widget might need. However, this doesn't seem to be efficient and will require too many models.</li> <li>Another option is to define the widget types somewhere (but where?) and then simplify the "widget_type" in the widgets table. (Therefore the table will contain "belongs_to :user" and "has_one :widget_type".) I will also have to have a field for the data the user entered (probably through XML?).</li> <li>Last option I can think of is to use Ruby classes to define the widgets. When the user customized the widget, I will create an object from the widget's class and store that object.</li> </ul> <p>Is there another better way?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload