[英]rails options_from_collection_for_select - display proper data in dropdown from API and user selects from dropdown
[英]Rails - options_from_collection_for_select - Display other fields based on what is currently highlighted in dropdown?
我有一個options_from_collection_for_select調用,它返回正確的集合。 許多記錄的名稱顯示空白值。 有沒有辦法在下拉菜單中顯示當前突出顯示的記錄的其他屬性?
您不僅限於數據庫中表示的對象的屬性,因此您可以簡單地在模型中定義一個替代方法,該方法返回名稱/如果名稱為nil則為替代屬性,例如:
class User
def name_or_alternative
name || alternative
end
end
然后從您的options_from_collection_for_select
調用此方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.