Rails 多对多 fields_for:如何访问 fields_for 值?
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 %>