Rails erb替代fields_for查找特定记录 [英] Rails erb alterantive to fields_for find specific record
问题描述
我是Rails的新手,正在尝试修改现有的应用程序.
I'm new to rails, and trying to modify an existing application.
我有一个与另一类型的记录"group_parameters"具有"has_many"关系的模型.组参数有两个字段(名称和值).
I have a model that has a 'has_many' relationship with another type of record 'group_parameters'. Group parameters has two fields (name and value).
我目前有一种形式,其范围为group_parameters并打印所有存储的记录.这是通过
I currently have a form that scopes group_parameters and prints all the stored records. This is achieved by doing
<%= f.fields_for group_parameters do | builder | %>
Attribute <%= builder.text_field(:name) %>
<% end %>
<%= f.fields_for group_parameters do | builder | %>
Attribute <%= builder.text_field(:name) %>
<% end %>
相反,我只想显示名称字段等于特定字符串的某些记录. 有一个简单的方法可以帮助您吗?
Instead I'd like to only display certain records where the name field equals a specific string. Is there a simple method to help with this?
推荐答案
<%= f.fields_for group_parameters, @specific_records do | builder | %>
Attribute <%= builder.text_field(:name) %>
<% end %>
根据您的条件从数据库中获取数据并分配给@specific_records
Fetch data from DB as per your criteria and assign to @specific_records
这篇关于Rails erb替代fields_for查找特定记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!