Storing data from dynamic forms

I am working on a dynamic form generator. Someone can create such fields as: string, text, boolean, number, file, etc.

Are there any gems or recommendations for storing data from such dynamic forms?

I mean, I can create several tables for each data type, or I can save them all as TEXTwith a flag whose type must be converted.

UPD

or am i better off using nosql here?

+3
source share
2 answers

I believe that Mongodb is the right choice for this application, since it does not apply any scheme, it is a good choice for arbitrary data.

, , . .

, (Ruby Mongoid code)

  class XForm
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

       field :name, :type => String
       field :user, :type => BSON::ObjectId     

       embeds_many :formfields
  end

 class Formfields
  include Mongoid::Document

     field :name, :type => String
     field :kind, :type => String
     #field :value, :type => String -> dont add it in formfields, make it dynamic sine the type varies

  embedded_in :xform
  end

, allow_dynamic_fields: true mongoid.yml

,

  form = XForm.new(:name=>'test form',:user => current_user.id)
   #for integer field
   form.formfields << Formfields.new(:name => "Age",:kind=>"Integer", :value => 21)
   #for bool field
   form.formfields << Formfields.new(:name => "isMarried",:kind=>"Boolean",:value => true)
   #for string field
   form.formfields << Formfields.new(:name => "name",:kind=>"String",:value => "ram")

,

+5

.

class User < ActiveRecord::Base
  [:field_1, :field_2, :field_3].each do |method|
    define_method method do
      workable_arbitrary_content[method]
    end

    define_method "#{method}=" do |value|
      data = workable_arbitrary_content.merge(method => value)
      self.arbitrary_content = YAML.dump(data)
    end
  end

  private
    def workable_arbitrary_content
      YAML.load(arbitrary_content || "") || {}
    end
end

3 , YAML. users arbitrary_content, .

.

describe User do
  context "virtual fields" do
    before(:each) do
      @user = User.new
    end

    it "should be able to handle strings" do
      @user.field_1 = "Data"
      @user.field_1.should eq("Data")
    end

    it "should be able to handle integers" do
      @user.field_2 = 1
      @user.field_2.should eq(1)
    end

    it "should be able to handle symbols" do
      @user.field_3 = :symbol
      @user.field_3.should eq(:symbol)
    end

    it "should be possible to override a field" do
      @user.field_3 = :symbol
      @user.field_3 = "string"

      @user.field_3.should eq("string")
    end

    it "should be possible to set more then one field" do
      @user.field_1 = :symbol
      @user.field_2 = "string"

      @user.field_1.should eq(:symbol)
      @user.field_2.should eq("string")
    end
  end
end
+2

All Articles