2010-10-11 119 views
2

希望有人能帮助我。目前正在开发一个需要一种奇怪功能的RoR项目。Rails,允许用户创建一个将被其他用户使用的表单

基本上,我有两种类型的用户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上设置验证时,我可以看到自己遇到了麻烦。

任何人有这个问题的任何经验?

+0

我想建立这样的事情,你发现什么是最好的方法? – JoeChin 2012-08-05 19:43:29

回答

0

我真的会更清楚地看待这个问题的方式使用单表继承。

您可以创建一个模型User,并在user.rb文件中定义研究人员和用户共有的所有验证和方法。

class User < ActiveRecord::Base 
    #all validations code and other logic related to users and researchers 
end 

创建另一个模型researcher.rb像

class Researcher < User 
    #create validations and logic code specific to researcher 
end 

会有只是一表 “用户”。并且会有一个额外的列(除了所有电子邮件,名称等),即“类型”列。此类型列指的是记录是研究人员还是用户。

这样定义

def can_fill_form? 
    return true 
end 

在researcher.rb一些方法,并在user.rb同样的方法类似

def can_fill_form? 
    return false 
end 

通过这样做,你就必须与所有用户和研究人员一个表在里面。像电子邮件,电话,密码等都是相同的,创建一个单独的表格在这里是多余的。

+0

感谢您的回答lakshmanan,但我想您可能误解了我的问题,该问题基于允许用户创建将由其他用户使用的表单的原则。 – stuartchaney 2010-10-11 22:59:57

+0

仍然没有接受者?... – stuartchaney 2010-10-19 09:46:54

0

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

我想有一个User模型has_manyForm模型。每个表单模型将有很多FormComponents。该FormComponent模型会对什么类型的表单元素是什么样的验证,默认值等领域

Researcher将担任由FormController的形式,将处理模型,并根据显示的形式在什么FormComponents出席。当Researcher完成表格时,我会将该答复打包为既属于Researcher也属于FormFormResponse

请记住,这只是这个想法如何发挥的一个粗略的想法,但在我看来,类似这样的方案将是处理它的最好方法。

相关问题