循环遍历嵌套字段

时间:2012-09-18 18:27:02

标签: ruby-on-rails ruby-on-rails-3.1 nested-attributes

我有以下型号

A listing has many services
A service has many questions and belongs to listing
A question has many answers
An answer belongs to question and listing

模型设置正确,效果很好。 某些服务有后续问题,例如“你为购买冰淇淋提供糖果折扣吗?”这些问题由管理员输入,这是输入的表格。

= simple_form_for(@listing, :url => service_answer_path, :html => {:class => "form-horizontal" }) do |f|
  - @questions.each do |q|
    = q.content
    = f.fields_for :service_answers do |s|
      = s.hidden_field :listing_id, :value => @listing.id
      = s.input :content
  = f.submit
它在首次提交时效果很好。但是当我尝试编辑时,它为同一个问题提供了多个答案字段。但是,这应该是在循环中运行f.fields_for。但是,如果我将其移出循环,则问题内容将不会显示在此处显示的每个答案文本字段上。 enter image description here
现在我如何将这些问题和答案一个接一个地做出来?

1 个答案:

答案 0 :(得分:0)

看到此链接可能会对您有帮助= Nested Model Form