Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic fields with Rails 3
    primarykey
    data
    text
    <p>I have a design issue I want to walk through.</p> <p>I'm building a Rails 3 app which will hold products from a variety of different companies. I'd like to define a large set of fields and each product can select the fields that are applicable to it.</p> <p>The field types will be single line text fields, multi line text fields, radio or select options, checkbox options, dates, durations or something more custom. I'll need to be able to dynamically render the fields based on this type for edit and show.</p> <p>My current idea is to use MongoDB and store everything in a Hash on the Product.</p> <pre><code>class Product include Mongoid::Document def self.field_definitions [{ :name =&gt; :code, :label =&gt; 'Code' }, { :name =&gt; :itinerary, :type =&gt; :multiline, :label =&gt; 'Itinerary', :category =&gt; :tour}, { :name =&gt; :infant_age, :type =&gt; :age_range, :label =&gt; 'Infante age range', :category =&gt; :tour}, ... ] end embedded_in :company field :field_data, type:Hash end </code></pre> <p>Then render the fields for new/edit something like:</p> <pre><code>= form_for Product.new do |f| = f.fields_for :field_data do |f| %ol - Product.field_definitions.each do |field_definition| %li = f.label field_definition[:name], field_definition[:label] = render "products/edit_fields/#{field_definition[:type] || 'singleline'}", :f =&gt; f, :field_definition =&gt; field_definition = f.submit "Create" </code></pre> <p>I then have a partial for each field type for edit and show.</p> <p>After creation, a Product might look like this in mongodb:</p> <pre><code>{"field_data":{ "itinerary": "FUN!", "code": "AHKDYK", "infant_age": { "max": 2, "min": 0 } }} </code></pre> <p>Is this a good approach?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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