ruby-on-rails - Rails,允许用户创建一个将被其他用户使用的表单

标签 ruby-on-rails ruby forms activerecord

希望有人能帮帮我。目前正在从事需要一种奇怪功能的 RoR 项目。

基本上,我有两种类型的用户,名为 User 和 Researcher。用户可以创建表格,这些表格存储在数据库中并由研究人员填写。

我有这个基本模式

create_table "form_fields", :force => true do |t|
    t.string   "name"
    t.string   "form_field_type"
    t.integer  "form_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "forms", :force => true do |t|
    t.integer  "visit_id"
    t.string   "name"
    t.integer  "form_category_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "researchers", :force => true do |t|
    t.string   "email",                               :default => "", :null => false
    t.string   "encrypted_password",   :limit => 128, :default => "", :null => false
    t.string   "password_salt",                       :default => "", :null => false
    t.string   "reset_password_token"
    t.string   "remember_token"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                       :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "researchers", ["email"], :name => "index_researchers_on_email", :unique => true
  add_index "researchers", ["reset_password_token"], :name => "index_researchers_on_reset_password_token", :unique => true

  create_table "results", :force => true do |t|
    t.integer  "form_id"
    t.integer  "subject_id"
    t.string   "form_field_name"
    t.string   "form_field_value"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
    t.string   "email",                               :default => "", :null => false
    t.string   "encrypted_password",   :limit => 128, :default => "", :null => false
    t.string   "password_salt",                       :default => "", :null => false
    t.string   "reset_password_token"
    t.string   "remember_token"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                       :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

  create_table "visits", :force => true do |t|
    t.string   "name"
    t.integer  "visit_order"
    t.integer  "study_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

因此,用户创建表单和 form_fields,然后研究人员登录并获得此表单

<% form_for [:researcher, @study, @subject, @visit, @form, Result.new] do |f| %> 

    <% @form.form_fields.each do |form_field| %>

      <%= f.label form_field.name, :index => form_field.id  %>
      <%= f.hidden_field :form_field_name, :value=>form_field.name, :index => form_field.id  %>
      <%= f.text_field :form_field_value, :index => form_field.id  %><br/>
      <%= f.hidden_field :form_id, :value=>@form.id, :index => form_field.id  %>
      <%= f.hidden_field :subject_id, :value=>@subject.id, :index => form_field.id  %>

    <% end %>


  <%= f.submit %>

 <% end %>

因此结果存储在结果表中,仅此而已。

当我试图允许用户在每个 form_field 上设置验证时,我发现自己遇到了麻烦。

有人遇到过这个问题吗?

最佳答案

下面是我将如何处理这个......

我会有一个 User 模型,has_many Form 模型。每个 Form 模型都有许多 FormComponentsFormComponent 模型将包含表单元素的类型、验证类型、默认值等字段。

Researcher 将由 FormController 提供表单,它将处理模型并根据存在的 FormComponents 显示表单.当 Researcher 完成表单时,我会将响应打包为 FormResponse,它同时属于该 ResearcherForm.

请记住,这只是这个想法将如何发挥作用的粗略想法,但在我看来,与此类似的方案将是处理它的最佳方式。

关于ruby-on-rails - Rails,允许用户创建一个将被其他用户使用的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3908685/

相关文章:

ruby-on-rails - Rails 中的胖模型 : How do I return hash (errors) or object (when success)

ruby-on-rails - Web 应用程序中的 API 版本控制

javascript - 选中两个复选框时启用输入提交按钮

javascript - 谷歌应用脚​​本侧边栏表单发送按钮不起作用

ruby - 将排序的 Ruby 数组转换为具有可能重复的排名

php - 基于提交数据的 Symfony2 表单验证组

Mysql2::错误:不正确的字符串值:'\xE2\x80\xA8\x09

ruby-on-rails - rails : PayPal Integration: "Load Configurations from specified file"?

ruby-on-rails - ActiveJob 和 Sidekiq 作业卡在队列中

ruby-on-rails - Rails 部分更新 ActionController::UnknownFormat