简单表单中的嵌套元素

Nested Elements within Simple Form

我正尝试在我的 Rails 4 应用程序中使用 Simple Form。

我有模型叫做 projects.rb 和 programs.rb。这两个模型中的每一个都有一些共同的组件。我使用另一个名为 scope.rb 的模型来处理这些问题。每个项目和项目群都有一个范围。

在范围内,我有名为 data.rb 和 materials.rb 的模型。

在我的项目视图中,我问了几个阈值问题。其中之一是你想要数据吗? (答案正确或错误)该问题的答案存储在我的范围模型中。如果答案为真,那么我想进一步询问有关数据的问题(答案存储在我的数据模型中)。

因此,关系是每个计划和项目都有一个范围。各数据资料均属于范围

目前,所有这些数据模型问题都在我的项目视图中列出。

在项目视图中设置所有内容后的项目视图如下:

这是一个范围问题,用于确定是否需要有关数据或材料的更多详细信息:

 <div class="col-md-3 col-md-offset-1">
              <%= f.label  'What sort resources are involved in this project?',   :class => 'question-project' %>
            </div>
           <div class="col-md-7">
              <div class="response-project" style="margin-left:5%">
                <%= f.simple_fields_for :scope do |s_all| %>
                 <%= s_all.input :if_datum, :as => :boolean, :label => false, inline_label: 'Data' %>
                 <%= s_all.input :if_material, :as => :boolean, :label => false,  inline_label: 'Equipment or materials' %>
                <% end %>
              </div>
             </div>
           </div>

如果数据答案为真,那我问:

  <div class="col-md-3 col-md-offset-1">
  </div>
  <div class="col-md-7">
    <%= f.simple_fields_for :scope do |data_s| %>
        <%= data_s.simple_fields_for :datum do |d| %>
            <%= d.label  'Is this data confidential or commercially sensitive?',   :class => 'response-project' %> <br>
            <%= d.collection_radio_buttons :confidential, [[true, '  Yes       '] ,[false, '     No']], :first, :last, {:item_wrapper_class => 'inline'}, {:class => "radio-inline response-project" } %>
            <%= d.input :prim_sec, label: 'What sort of data do you want?', label_html: {class: 'response-project'}, collection: ["Primary", "Secondary", "Both" ], prompt: "Choose one", item_wrapper_class: 'inline' %>
        <% end %>
    <% end %>
  </div>
</div>
</div>

一切正常,只是我想在我的程序模型中使用相同的表单。这就是为什么我试图将它们提取到我的数据模型中的部分视图中,以便我可以在项目和程序模型中使用它们。

当我从 data/_form 视图开始时,提取了这个:

<div class="row">
  <div class="col-md-3 col-md-offset-1">
  </div>
  <div class="col-md-7">
    <%= f.simple_fields_for :scope do |data_s| %>
        <%= data_s.simple_fields_for :datum do |d| %>
            <%= d.label  'Is this data confidential or commercially sensitive?',   :class => 'response-project' %> <br>
            <%= d.collection_radio_buttons :confidential, [[true, '  Yes       '] ,[false, '     No']], :first, :last, {:item_wrapper_class => 'inline'}, {:class => "radio-inline response-project" } %>
            <%= d.input :prim_sec, label: 'What sort of data do you want?', label_html: {class: 'response-project'}, collection: ["Primary", "Secondary", "Both" ], prompt: "Choose one", item_wrapper_class: 'inline' %>
        <% end %>
    <% end %>
  </div>
</div>
</div>

并在我的项目视图中替换该代码以呈现 "data/form"。

我的问题是,当我将数据代码移动到 data/_form 以询问数据特定问题时,出现如下错误。当我尝试这样做时,我的看法是:

#<#:0x00000107ff3a90> 的未定义局部变量或方法“f”

这是对 data/_form 视图中的一行的引用:

    <%= f.simple_fields_for :scope do |data_s| %>

有人知道我做错了什么吗?

谢谢

我假设你正在做这样的事情:

<%= simple_form_for project do |f| %>
  <%= render 'data/form' %>
<% end %>

渲染部分时,不会向下传递局部变量。因此,如果您需要在 data/_form 部分中访问 f,则需要为您的部分提供相应的局部变量。试试这个:

<%= simple_form_for project do |f| %>
  <%= render 'data/form', f: f %>
<% end %>

或者,以更详细的方式:

<%= simple_form_for project do |f| %>
  <%= render partial: 'data/form', locals: {f: f} %>
<% end %>

HTH!