首页 > 解决方案 > Rails collection_select 不同的选定属性

问题描述

我有collection_select来自其他模型的选项,我需要在下拉列表和选定值中显示不同的属性。即我需要在下拉列表中显示,object.title但选择的值应该显示object.code。我有一个代码:

f.collection_select :some_attribute_id, Country.all, :id, :title

当前在下拉列表中显示值和显示选定值是相同的属性。我试图做类似的事情

f.collection_select :some_attribute_id, Country.all, :id, :title, { prompt: @object.country.code }

selected: @object.country.code但不成功

标签: ruby-on-railscollection-select

解决方案


推荐阅读