【问题标题】:Nested Elements within Simple Form简单表单中的嵌套元素
【发布时间】:2015-05-17 05:04:37
【问题描述】:

我正在尝试将 Simple Form 与我的 Rails 4 应用程序一起使用。

我有名为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 以询问数据特定问题时,我得到了下面列出的错误。我的观点是,当我尝试这样做时:

#

的未定义局部变量或方法 `f'

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

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

有谁知道我做错了什么?

谢谢

【问题讨论】:

    标签: ruby-on-rails simple-form


    【解决方案1】:

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

    <%= 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!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      相关资源
      最近更新 更多