Rails fields_for in fields_for不与父ID相关联的行

时间:2016-01-17 18:50:53

标签: ruby-on-rails

我正在尝试在表单底部添加一些自定义问题。但是,问题的ID与答案字段无关。我该怎么做呢?

input.html.erb

<% @event.event_questions.each do |question| %>
  <%= f.simple_fields_for :event_question, question do |answer| %>
    <%= answer.simple_fields_for :event_answer do |e| %>
      <%= e.input :answer, label: question.question %>
    <% end %>
  <% end %>
<% end %>

output.html

<div class="form-group string required participant_event_question_event_answer_answer"><label class="string required control-label" for="participant_event_question_event_answer_answer"><abbr title="required">*</abbr> Goals</label><input class="string required form-control" type="text" name="participant[event_question][event_answer][answer]" id="participant_event_question_event_answer_answer" /></div>
<div class="form-group string required participant_event_question_event_answer_answer"><label class="string required control-label" for="participant_event_question_event_answer_answer"><abbr title="required">*</abbr> Action Plan</label><input class="string required form-control" type="text" name="participant[event_question][event_answer][answer]" id="participant_event_question_event_answer_answer" /></div>
<div class="form-group string required participant_event_question_event_answer_answer"><label class="string required control-label" for="participant_event_question_event_answer_answer"><abbr title="required">*</abbr> Vision</label><input class="string required form-control" type="text" name="participant[event_question][event_answer][answer]" id="participant_event_question_event_answer_answer" /></div>

1 个答案:

答案 0 :(得分:0)

假设question_id表中有answers,您可以尝试以下代码。

<% @event.event_questions.each do |question| %>
  <%= f.simple_fields_for :event_question, question do |answer| %>
    <%= answer.simple_fields_for :event_answer do |e| %>
      <%= e.input :answer, label: question.question %>
      <%= e.input :question_id, as: :hidden, input_html: { :value => question.id }
    <% end %>
  <% end %>
<% end %>