Ruby on Rails - 从自定义表单获取用户输入

时间:2013-07-27 18:59:23

标签: ruby-on-rails ruby-on-rails-3 forms input user-input

我有两个表QuestionAnswer,其中在问题模型中有一行定义了两个表之间的关联has_many Answer和答案模型:{{1} }。

belongs_to Question表中,我有以下列:Questionsidtitlesubtitle

question_type表中,我只有两列:Answersquestion_id

我在_form.html.erb中设置了表单,以便获得预定义的问题集(例如,问题ID 1,2,5,6,11)。此外,表单将根据该集合动态创建表单所需的内容。以下代码就是我所做的:

(其中text是一个存储预定义问题集的数组和与问题相关的答案。每个问题都是一个OpenStruct,存储相应问题的所有相关信息)

set

现在的问题是,当我点击提交按钮时,我将如何根据问题ID获取表单中的所有用户输入?

我正在考虑一个问题存在多个相同类型和多个答案字段的问题。或者有更好的方法,也可以实现我想要的目标吗?

编辑:这是由上面的ruby嵌入代码生成的HTML代码,如果有帮助的话。

<% field_names = Array.new %>
<% set.each do |question| %>
    <%= f.label question.question %>
    <%= f.label question.question_subtitle %>
    <% case question.question_type %>
        <% when "check box" %>
            <%= f.label question.question_type %>
            <% question.answers.each do |answer| %>
                <%= check_box_tag(answer.answer_id) %>
                <%= label_tag(question.id, answer.text) %>
                <%
                   field_names.append(params[answer.answer_id])
                %>
            <% end %>

        <% when "text field" %>
            <%= f.label question.question_type %>
            <% question.answers.each do |answer| %>
                <%= answer.text %>
                <%= text_field(question.question_id, answer.answer_id) %>
                <%
                   s = ((question.question_id).to_s + "[" + (answer.answer_id).to_s + "]")
                   field_names.append(params[s])
                %>
            <% end %>

        <% when "scale" %>
            <%= f.label question.question_type %>
            <%
               range = Array.new
               question.answers.each do |answer|
                 range.append(answer.text)
               end
               field_names.append(params[question.answers[0].answer_id])
            %>
            <%= select_tag(question.answers[0].answer_id, options_for_select(range[0]..range[1])) %>
    <% end %>
    <br/>
<% end %>

编辑:

我稍微更改了代码,以便现在每个字段使用不同的名称,而不是所有字段都使用相同的名称,如果它们具有相同的属性。但是名称取决于答案ID,如果是文本字段,那么它将是答案ID和问题ID。

这样,我想,也许我可以使用 <label for="tracker_question 1">Question 1</label> <label for="tracker_question 1 Subtitle">Question 1 subtitle</label> <label for="tracker_check box">Check box</label> <input id="1" name="1" type="checkbox" value="1" /> <label>answer 1-1</label> <input id="2" name="2" type="checkbox" value="1" /> <label>answer 1-2</label> <br/> <label for="tracker_question 2">Question 2</label> <label for="tracker_question 2 Subtitle">Question 2 subtitle</label> <label for="tracker_text field">Text field</label> answer 2-1 <input id="11_3" name="11[3]" size="30" type="text" /> answer 2-2 <input id="11_4" name="11[4]" size="30" type="text" /> <br/> <label for="tracker_question 3">Question 3</label> <label for="tracker_question 3 Subtitle">Question 3 subtitle</label> <label for="tracker_scale">Scale</label> <select id="5" name="5"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> </select> 重定向来访问这些字段,因为它会生成这样的哈希:

before_save

因此,我创建了另一个名为{"utf8"=>"✓", "authenticity_token"=>"DgdKpdecD+jmq1EvAIPxzjFxHGOUkJXoE10VpCBtxqU=", "tracker"=>{"patient_id"=>"1"}, "1"=>"1", "11"=>{"3"=>"test1", "4"=>"test2"}, "5"=>"6", "commit"=>"Create Tracker"} 的变量来获取字段名称,但我似乎无法按照我想要的方式工作。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

您有多个资源表单。据我了解,你想保存几个答案。我想这是因为你正在渲染问题的所有答案,所以我想你不是在创建问题和答案的部分。

如果是这种情况,我会创建一个保存此信息的模型,很难命名:

class Item < ActiveRecord:Base
  belongs_to :question
  belongs_to :answer
end

以及将这些项目分组的模型:

class Test < ActiveRecord::Base
  belongs_to :user
  has_many   :items

  accepts_nested_attributes_for :items

  attr_accessible :items_attributes
end

你没有提到user

然后我的表格就像:

<%= form_for @test do |test| %>

  <%= test.fields_for :items do |item|  %>
    <%# Render question ----> your code %>

    <%= item.hidden_field :question_id %>
  <% end %>
<% end %>

你的控制器:

class TestsController < ApplicationController
  def new
    @test = Test.new question_ids: params[:question_ids]
  end

  def create
    @test = Test.new params[:test]

    if @test.save
      # success
    else
      # fail
    end
  end
end

请注意,您将发送params[:question_ids]来构建表单。要做到这一点:

class Test < ActiveRecord::Base
  ...
  attr_accessible :question_ids

也许我错过了问题的全部内容。