Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 存储动态表单中的数据_Ruby On Rails_Ruby_Forms_Activerecord_Dynamic Forms - Fatal编程技术网

Ruby on rails 存储动态表单中的数据

Ruby on rails 存储动态表单中的数据,ruby-on-rails,ruby,forms,activerecord,dynamic-forms,Ruby On Rails,Ruby,Forms,Activerecord,Dynamic Forms,我正在开发动态表单生成器。有人可以创建如下字段:字符串、文本、布尔值、数字、文件等 对于存储此类动态表单中的数据,有什么建议或指导原则吗? 我的意思是,我可以为每个数据类型创建许多表,或者我可以将所有表存储为文本,并标记它应该转换的类型 UPD 或者我最好在这里使用nosql?我喜欢这种方法 class User < ActiveRecord::Base [:field_1, :field_2, :field_3].each do |method| define_method

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

对于存储此类动态表单中的数据,有什么建议或指导原则吗?

我的意思是,我可以为每个数据类型创建许多表,或者我可以将所有表存储为
文本
,并标记它应该转换的类型

UPD

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

我喜欢这种方法

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

我相信Mongodb是这个应用程序的正确选择,因为它不强制任何模式,它是任意数据的好选择

同样,它也支持您所期望的所有数据类型。所以这很容易

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

要将值字段添加为动态字段,需要在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")
form=XForm.new(:name=>testform',:user=>current_user.id)
#对于整数字段
form.formfields“Age”,:kind=>“Integer”,:value=>21)
#布尔场
form.formfields“isMarried”,:kind=>“Boolean”,:value=>true)
#对于字符串字段
form.formfields“name”,:kind=>“String”,:value=>“ram”)

希望这有助于

有趣的方法,表明我们应该在这里使用NoSQL来存储数据结构:)。谢谢@fl00r正是:)我认为NoSQL就是这种情况下的狗屎。谢谢你的回复!我将单独存储表单结构和表单值。因为很多人会填表格。我更喜欢使用MongoMapper:)。无论如何,谢谢你的方法,它很有用
  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
  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")