Ruby on rails 从一个模型的问题获取数据库驱动,并将答案保存到另一个模型

Ruby on rails 从一个模型的问题获取数据库驱动,并将答案保存到另一个模型,ruby-on-rails,forms,ruby-on-rails-4,Ruby On Rails,Forms,Ruby On Rails 4,我正在尝试使用rails创建一个灵活的数据库驱动表单系统,但在使用这种方法生成实际表单时遇到了一些困难 我处理它的方式是使用一个模型作为表单的索引(称之为MyForms),一个模型处理所有表单的问题(称之为MyQuestions),一个模型处理表单的所有答案(称之为MyAnswers)。表格和问题已按以下方式存储到数据库中: mysql> select * from my_forms;

我正在尝试使用rails创建一个灵活的数据库驱动表单系统,但在使用这种方法生成实际表单时遇到了一些困难

我处理它的方式是使用一个模型作为表单的索引(称之为
MyForms
),一个模型处理所有表单的问题(称之为
MyQuestions
),一个模型处理表单的所有答案(称之为
MyAnswers
)。表格和问题已按以下方式存储到数据库中:

mysql> select * from my_forms;                                                                                                                                                   
+----+------------+---------+---------------------+---------------------+                                                                                                           
| id | title      | company | created_at          | updated_at          |                                                                                                           
+----+------------+---------+---------------------+---------------------+
|  1 | First_Form |       1 | 2014-11-20 20:58:53 | 2014-11-20 20:58:53 |
+----+------------+---------+---------------------+---------------------+


mysql> select * from questions;
+----+--------------+-----------------+---------------+---------------------------------------------------------------------+---------------------+---------------------+
| id | my_form_id | question_number | question_type | the_question                                                        | created_at          | updated_at          |
+----+--------------+-----------------+---------------+---------------------------------------------------------------------+---------------------+---------------------+
|  1 |            1 |               1 | string        | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
|  2 |            1 |               2 | radio         | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
|  3 |            1 |               3 | check         | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
|  4 |            1 |               4 | radio         | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
|  5 |            1 |               5 | text-box      | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
|  6 |            1 |               6 | string        | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
|  7 |            1 |               7 | string        | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
|  8 |            1 |               8 | text-box      | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
|  9 |            1 |               9 | check         | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
| 10 |            1 |              10 | check         | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
+----+--------------+-----------------+---------------+---------------------------------------------------------------------+---------------------+---------------------+
答案数据库(当前为空)的结构如下:

mysql> show columns in answers;
+-----------------+----------+------+-----+---------+----------------+
| Field           | Type     | Null | Key | Default | Extra          |
+-----------------+----------+------+-----+---------+----------------+
| id              | int(11)  | NO   | PRI | NULL    | auto_increment |
| question_id     | int(11)  | YES  | MUL | NULL    |                |
| user_id         | int(11)  | YES  |     | NULL    |                |
| question_number | int(11)  | YES  |     | NULL    |                |
| the_answer      | text     | YES  |     | NULL    |                |
| created_at      | datetime | YES  |     | NULL    |                |
| updated_at      | datetime | YES  |     | NULL    |                |
+-----------------+----------+------+-----+---------+----------------+
这样,无论我想发布多少表单、问题/表单,或者他们的要求是什么,我都能以最少的麻烦让它们启动并运行

现在,我可以在
legal\u forms\u controller.rb中使用
show
很容易地显示问题,方法如下:

def show
  @legalform = LegalForm.find(params[:id])
  @questions = @legalform.questions
end
然后在
show.html.erb
中显示它们,如下所示

<% @questions.each do |client| %>
    <span><%= client.the_question %></span><br/>
<% end %>



但我正在努力找出如何将这些字段与表单输入字段配对,并将它们保存到answers数据库中。我认为获得一张空白的答案板可能需要创建一个答案控制器,然后使用
新的
方法,然后我可以使用一些条件在视图中创建适当的输入类型,给定
问题类型
。然而,我不太明白如何有效地创建这个表单,然后根据这种方法保存数据。在此方面的任何帮助都将不胜感激。(当然,“上帝啊,你为什么要这样做,你应该这样做……”的回答也是可以接受的。)

我将其建模为:

# column :label, TEXT - the text body of the question
# column :input_type, VARCHAR - maps to html input types
# ...
class Question 
    has_many :answers # since there may be several acceptable answers or options
end

# column :is_correct, BOOL
# column :text, TEXT
class Answer
    belongs_to :question
end

# Or test 
class Test
    has_many :questions
    has_many :test_scores

    # generate a test with random questions
    def self.random(seed = nil)
        # ...
    end
end

class TestScore
    belongs_to :user
    belongs_to :test
end
这允许我们重用任何测试中的任何问题,并通过
test
user
查询分数(前提是您有某种用户模型)

您还可以构建UI,让管理员创建测试、问题和答案

要管理测试,请使用TestScoreController:

class TestScoreController
    # administer a test
    def new
        @score = TestScore.new(user_id: current_user)
        @score.test = @test = Test.random()
    end

    # corrects test
    def create
        # ...
    end

    # Show a test result
    def show
       @score = TestScore.find(params[:id]).joins(:test)
    end
end
以及视图:
new.html.erb

<%= form_for(@score) do %>
    <%= @test.questions.each do |q| %>
        <%= f.fields_for :questions, question do |q| -%>
            <%= f.label q.label %>
            <%= QuestionHelper.input(q) %>
            <%= q.hidden_field :test_score_id, @score.id %>
        <%= end %>
    <%= end %>
<%= end %>

这段代码是作为一个粗略的草图-它完全没有经过测试,甚至不能保证运行!哇,谢谢你,伙计!我会在今天晚些时候或者明天打个盹儿,这样我就有时间去理解它了。非常感谢!
# app/helpers/question_helper
module QuestionHelper
    def self.input(question)
        case question.input_type.to_sym
        when :text
        when :radio_button
        # ..    
        end
    end
end