1

我正在使用以下数据模型创建调查:MathTest has_many math_questions

MathTest#update想为每个问题创建一个表格。表单应如下所示:

<%= form_for(mathtest) do %>
  <% math_question.each do |question| %>
    <%= f.label :answer %>
    <%= f.text_field :answer %>
    <%= f.hidden_field math_question.id %>
  <% end %>

  <% f.submit %>
<% end %>

我想向 MathTest 控制器发送一组带有 math_question id 和该问题答案的元组。然后在控制器中,我可以调用另一个方法来评估每个问题的答案。

如何编写表单以发送适当的元组?

4

1 回答 1

1

这应该这样做:

<%= form_for(mathtest) do |f| %>
  <% math_question.each do |question| %>
    <%= f.fields_for :answer do |ff| %>
      <%= ff.label :answer %>
      <%= ff.text_field :answer %>
    <% end %>
  <% end %>

  <% f.submit %>
<% end %>

同时编辑模型:

class MathTest < ActiveRecord::Base
  has_many :math_questions
  accepts_nested_attributes_for :math_questions

fields_for 文档:http ://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

于 2013-11-12T22:05:58.863 回答