在 fields_for 循环中获取对象字段的值 [英] Get a value of object field inside fields_for loop
问题描述
在以下场景中,我需要检查 fields_for
循环中对象属性的值.
In the following scenario, I need to check the value of the object property in the fields_for
loop.
<%= f.semantic_fields_for :review_details do |rd| %>
<%= rd.input :review_criteria_id, :as=>:hidden %>
<% end %>
在循环中,:review_criteria_id
呈现为隐藏字段,但我有一个场景,如果它是特定标准,我必须打印更多信息.如何在循环中获取 review_criteria_id
的值.我用过:
As in the loop, :review_criteria_id
is rendered as hidden field, but I have a scenario, where I have to print some more information if it is a specific criteria. How can I get the value of review_criteria_id
in the loop.
I used:
rd.review_criteria_id
但是由于rd
是formtastic 对象,所以我无法获取:review_crieteria_id
的值.
But since rd
is the formtastic object, so I couldn't get the value of :review_crieteria_id
.
推荐答案
Formtastic 为 Rails 代码添加了额外的功能,但不会删除现有的功能,因此以下内容应该适合您:
Formtastic adds additional features to the Rails code, but doesn't take away existing functionality so the following should work for you:
rd.object.review_criteria_id
'object' 可以在普通的 Rails 表单助手中使用来访问底层绑定对象,Formtastic 遵循这个约定.
'object' can be used in plain Rails form helpers to access the underlying bound object, and Formtastic honours this convention.
这篇关于在 fields_for 循环中获取对象字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!