简体   繁体   English

Railsfields_for在fields_for行中不与父ID关联

[英]Rails fields_for in fields_for rows not associating with parent id

I am trying to add some custom questions to the bottom of my form. 我正在尝试在表单底部添加一些自定义问题。 However the id of the question isn't being associated with the answer field. 但是,问题的ID未与答案字段相关联。 How would I go about this? 我将如何处理?

input.html.erb 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 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>

Assuming you have question_id in answers table, you can try the below code. 假设您在answers表中有question_id ,则可以尝试以下代码。

<% @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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM