Ruby on rails 如何将另一个属性附加到集合\u select?
我正在尝试向当前集合添加一个布尔属性(第三方),该集合当前仅显示名称:Ruby on rails 如何将另一个属性附加到集合\u select?,ruby-on-rails,collection-select,Ruby On Rails,Collection Select,我正在尝试向当前集合添加一个布尔属性(第三方),该集合当前仅显示名称: <div class='form-group'> <%= f.label :project_component_id, class: "form-label" %> <%= f.collection_select :project_component_id, issue.project.project_components.order("LOWER(name)"), :id, :name
<div class='form-group'>
<%= f.label :project_component_id, class: "form-label" %>
<%= f.collection_select :project_component_id, issue.project.project_components.order("LOWER(name)"), :id, :name, {include_blank:true}, class: "form-control input-sm" %>
</div>
如何附加第三方,使每个选择选项显示为“名称(第三方)”
谢谢 您可以在模型中创建一个实例方法,该方法使用额外的文本插入所需的属性,并将其作为第四个选项传递给您的
集合\u select
助手:
我假设它被称为ProjectComponent,因此:
class ProjectComponent < ApplicationRecord
def name_with_thirdparty
"#{name}(#{third_party})"
end
class ProjectComponent
因此,在你看来:
<%= f.collection_select(
:project_component_id,
issue.project.project_components.order("LOWER(name)"),
:id,
:name_with_thirdparty,
{ include_blank: true },
class: 'form-control input-sm') %>
应该是name+”({third\u party})
这太棒了-谢谢!我只是稍微更新了一下,以得到我想要的,但你明白我的目的“#{name}(#{third_party})”
有效,但也谢谢@salilu更新了答案。谢谢两位。