Rails -- fields_for 不起作用?

发布于 2024-11-30 01:35:26 字数 1334 浏览 3 评论 0原文

当我运行 Rails 服务器并转到网站上的学校/新页面时,会出现带有标签“学校”的字段,我可以在其中输入学校名称,但 fields_for 下的所有其他字段用于输入学校管理员的信息不会出现在我的网站上——当我在表单上使用“检查元素”时,它们就好像不存在一样。为什么它们没有出现在我的页面上?

<%= form_for(@school) do |f| %>
  <% if @school.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@school.errors.count, "error") %> prohibited this school from being saved:</h2>

      <ul>
      <% @school.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :school %><br />
    <%= f.text_field :name %>
  </div>
  <%= f.fields_for :admin do |f2| %>
    <div class="field">
      <%= f2.label "administrator name" %><br />
      <%= f2.text_field :name %>
    </div>
    <div class="field">
      <%= f2.label "administrator email" %><br />
      <%= f2.text_field :email %>
    </div>
    <div class="field">
      <%= f2.label "administrator gender" %><br />
      <%= f2.text_field :gender %>
    </div>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

When I run the rails server and go to the school/new page on my site, the field with the label "school" where I can enter the school's name appears, but all the other fields under fields_for which are for entering the school administrator's info do not show up on my site -- when I use "inspect element" on my form it is like they are not even there. Why aren't they appearing on my page?

<%= form_for(@school) do |f| %>
  <% if @school.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@school.errors.count, "error") %> prohibited this school from being saved:</h2>

      <ul>
      <% @school.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :school %><br />
    <%= f.text_field :name %>
  </div>
  <%= f.fields_for :admin do |f2| %>
    <div class="field">
      <%= f2.label "administrator name" %><br />
      <%= f2.text_field :name %>
    </div>
    <div class="field">
      <%= f2.label "administrator email" %><br />
      <%= f2.text_field :email %>
    </div>
    <div class="field">
      <%= f2.label "administrator gender" %><br />
      <%= f2.text_field :gender %>
    </div>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绾颜 2024-12-07 01:35:26

TLDR:

出于我的目的,我必须确保表单对象的子关联已填充新记录。最初,父对象的子关联返回 nil。我只需要实例化一个对象并且表单就可以正确呈现。


如果有人从谷歌到达这里,我遇到了与海报相同的问题,但接受的答案对我没有帮助。

首先,一些背景知识。当您使用普通的 fields_for 而不是 f.fields_for 时,字段的呈现方式会有所不同。普通版本(如上所述)不会将呈现的字段与表单的对象相关联。查看不同的输出:

fields_for

form_for @parent_instance do |f|
  f.text_field :parent_field
  fields_for :child_obj do |child_fields|
    child_fields.text_field :child_field
  end
end

Renders

<input id="parent_class_parent_field" name="parent_class[parent_field]" type="text" />
<input id="child_class_child_field" name="child_class[child_field]" type="text" />

哪些 Rails 将解析为这些参数:

{ "parent_class" => {
      "parent_field" => "Parent field value"
    },
  "child_class" => {
      "child_field" => "Child field value"
    }
}

f.fields_for

form_for @parent_instance do |f|
  f.text_field :parent_field
  f.fields_for :child_obj do |child_fields|
    child_fields.text_field :child_field
  end
end

Renders

<input id="parent_class_parent_field" name="parent_class[parent_field]" type="text" />
<input id="parent_class_child_class_child_field" name="parent_class[child_class][child_field]" type="text" />

哪些 Rails 将解析为这些参数:

{ "parent_class" => {
      "parent_field" => "Parent field value",
      "child_class" => {
          "child_field" => "Child field value"
        }
    }
}

区别在于 当您使用 时,子对象的参数仅嵌套在父对象下>f.fields_for 因此,使用已接受答案的建议(使用裸 fields_for)无法创建关联记录。

事实证明,f.fields_for 没有渲染任何内容的原因是子关联为零。为了修复它并让我的表单正确渲染并以正确嵌套的方式返回参数,我必须在控制器中实例化该关联,如下所示:

def new
  @parent = ParentClass.new
  @parent.child_obj = ChildClass.new
end

TLDR:

For my purposes, I had to make sure the form object's child association was populated with a new record. Originally, the parent object's child association returned nil. I just had to instantiate an object and the form rendered correctly.


In case anyone arrives here from Google, I had a the same problem as the poster, but the accepted answer didn't help me.

First, some background. When you use the plain fields_for instead of f.fields_for, the fields are rendered differently. The plain version (as suggested above) does not associate the fields rendered with the form's object. Look at the different outputs:

fields_for

form_for @parent_instance do |f|
  f.text_field :parent_field
  fields_for :child_obj do |child_fields|
    child_fields.text_field :child_field
  end
end

Renders

<input id="parent_class_parent_field" name="parent_class[parent_field]" type="text" />
<input id="child_class_child_field" name="child_class[child_field]" type="text" />

Which Rails will parse into these parameters:

{ "parent_class" => {
      "parent_field" => "Parent field value"
    },
  "child_class" => {
      "child_field" => "Child field value"
    }
}

f.fields_for

form_for @parent_instance do |f|
  f.text_field :parent_field
  f.fields_for :child_obj do |child_fields|
    child_fields.text_field :child_field
  end
end

Renders

<input id="parent_class_parent_field" name="parent_class[parent_field]" type="text" />
<input id="parent_class_child_class_child_field" name="parent_class[child_class][child_field]" type="text" />

Which Rails will parse into these parameters:

{ "parent_class" => {
      "parent_field" => "Parent field value",
      "child_class" => {
          "child_field" => "Child field value"
        }
    }
}

The difference is that the child object's parameters only get nested under the parent when you use f.fields_for. So using the accepted answer's suggestion of using the bare fields_for didn't work for creating an associated record.

Turned out, the reason f.fields_for wasn't rendering anything was because the child association was nil. To fix it and have my form render correctly and return the parameters in the properly nested way, I had to instantiate that association like this in my controller:

def new
  @parent = ParentClass.new
  @parent.child_obj = ChildClass.new
end
年少掌心 2024-12-07 01:35:26
 <%= f.fields_for :admin do |f2| %>

尝试删除 f。

尝试这样做:

<%= fields_for :admin do |f2| %>

有两种使用 field_for 的方法:

一种通用方法。 (就像我的例子)。

一个嵌套的。

要使用嵌套的 field_for (f.field_for),您的模型对象必须正确设置

例如:与 @school 和 @admin 的关系

class School< ActiveRecord::Base
  has_one :admin
  accepts_nested_attributes_for :admin
end

最后,有多种使用 field_for 的方法,具体取决于您如何在模型对象上设置关系。

指南详细解释了所有可能的设置。您可能会发现为什么它不适合您。

我敢打赌,您的 Admin 和 School 模型对象不存在关系,或者您没有将 Accept_nested_attributes_for :admin 放入您的 School 对象中。

 <%= f.fields_for :admin do |f2| %>

try removing the f.

try with just this:

<%= fields_for :admin do |f2| %>

There is two way to use field_for:

A generic one. (like my example).

A nested one.

To use the nested field_for (f.field_for), your model object must be setup correctly

Ex: Relation with @school and @admin

class School< ActiveRecord::Base
  has_one :admin
  accepts_nested_attributes_for :admin
end

In the end, there is multiple ways of using field_for depending on how you setup the relations on your model object.

This guide explain in details all the possible setup. You might find why it was not working for you.

My bet is your Admin and School model object are not in a relation, or you didn't put the accepts_nested_attributes_for :admin in your School object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文