存储动态表单中的数据

发布于 2024-11-15 12:29:54 字数 234 浏览 3 评论 0原文

我正在研究动态表单生成器。有人可以创建诸如:字符串、文本、布尔值、数字、文件等字段。

是否有任何宝石或指南用于存储此类动态表单中的数据?

我的意思是我可以为每种数据类型创建多个表,或者我可以将它们全部存储为 TEXT ,并带有应该转换的标志类型。

UPD

或者我最好在这里使用 nosql ?

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

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

I mean I can create a number of tables for each datatype, or I can store all of them as a TEXT with flag wich type it should be converted.

UPD

or I'd better use nosql here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红尘作伴 2024-11-22 12:29:54

我相信 Mongodb 是这个应用程序的正确选择,因为它不强制任何模式,对于任意数据来说它是一个不错的选择。

此外,它确实支持您期望的所有数据类型。所以这很容易。

有一个如下所示的表单集合(Ruby Mongoid 代码)

  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

要将值字段添加为动态字段,您需要在 mongoid.yml 中启用 allow_dynamic_fields: true

并创建一个像这样的新字段

  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")

希望这样帮助

I believe Mongodb is a right choice for this application, since it doesn't enforce any schema, its a good choice for arbitrary data.

As well, it does support all the datatype you have expected. so it is easy.

Have a form collection which looks like this (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

and

 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

To add value field as a dynamic field, you need to enable allow_dynamic_fields: true in mongoid.yml

and create a new field like this

  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")

Hope this helps

傲影 2024-11-22 12:29:54

我喜欢这种方法。

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 中创建一个名为 argumentary_content 的字段,其类型为 text

以下是上述代码的一些规格。

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

I like this approach.

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

I this case you create 3 virtual fields that is being saved as YAML.
Create a field in the users called arbitrary_content that is of type text.

Here are some specs for the code above.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文