Rails 多对多 fields_for:如何访问 fields_for 值?

发布于 2024-09-27 20:07:40 字数 1078 浏览 4 评论 0原文

我正在尝试在 Rails 3 中创建一组嵌套(多对多)表单。感谢 fields_for,一切正常,但我需要在每个嵌套表单上方放置一个标题。该标题具有每个相应嵌套表单中的 professional_type.name 字段的值(该字段具有预填充值)。

我花了很长时间从 fields_for 表单对象中提取 professional_type.name 值。我需要知道的是:

a.)是否可以从 fields_for 对象中提取预填充的值,如果可以,如何提取?

b.) 如何仅对每个 |specform| 的相关“fields_for”条目进行多对多嵌套表单循环值,而不是全部?

非常感谢任何使用任一方法(或替代方法)的建议。

<% @professional.professional_specialties.each do |specform| %>
    <%= specform.profession_type.name %>

    <% f.fields_for :professional_specialties do |specialtyform| %>
        <%= specialtyform.label :profession_type %>
        <%= specialtyform.text_field :profession_type %>
        <%= specialtyform.label :qualifications %>
        <%= specialtyform.text_field :qualifications %>
        <%= specialtyform.label :license_number %>
        <%= specialtyform.text_field :license_number %>
        <%= specialtyform.label :enabled %>
        <%= specialtyform.check_box :enabled %>
    <% end %>
<% end %>

I'm trying to created a set of nested (many-to-many) forms in rails 3. Everything works fine thanks to fields_for, but I need to put a title above each nested form. That title has the value of the profession_type.name field (which has a prepopulated value) in each respective nested form.

I'm having a heckuva time extracting that profession_type.name value from fields_for form objects. What I need to know is either:

a.) Is it possible to extract prepopulated values from fields_for objects, and if so how?

or

b.) How can I make a many-to-many nested form loop over only the relevant "fields_for" entry for each |specform| value, instead of all of them?

Any suggestions using either method (or alternative approaches) very much appreciated.

<% @professional.professional_specialties.each do |specform| %>
    <%= specform.profession_type.name %>

    <% f.fields_for :professional_specialties do |specialtyform| %>
        <%= specialtyform.label :profession_type %>
        <%= specialtyform.text_field :profession_type %>
        <%= specialtyform.label :qualifications %>
        <%= specialtyform.text_field :qualifications %>
        <%= specialtyform.label :license_number %>
        <%= specialtyform.text_field :license_number %>
        <%= specialtyform.label :enabled %>
        <%= specialtyform.check_box :enabled %>
    <% end %>
<% end %>

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

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

发布评论

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

评论(1

攒眉千度 2024-10-04 20:07:40

FormBuilder 具有属性访问器,例如 :object_name 和 :object。

对于您的特定问题,请尝试使用:
<%= specform.object.name %>

The FormBuilder has attribute accessors like :object_name and :object.

For your particular issue try using:
<%= specform.object.name %>

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