我需要帮助。如何删除":已过期" simple_form集合的价值?
以下是代码:
class ItemExpense < ApplicationRecord
enum expense_type: [:expired, :damaged, :lost]
end
<%=f.input :expense_type, collection: ItemExpense.expense_types.keys.to_a.map {|i| [i.titleize, i]}, as: :radio_buttons %>
答案 0 :(得分:1)
您可以创建一个类方法
class ItemExpense < ApplicationRecord
enum expense_type: [:expired, :damaged, :lost]
BLACKLISTED_TYPES = ['expired']
def self.whitelisted_expense_types
expense_types.keys - BLACKLISTED_TYPES
end
end
此外,如果您可以创建辅助方法
,那就太棒了# app/helpers/application_helper.rb
module ApplicationHelper
def whitelisted_expense_types
ItemExpense.whitelisted_expense_types.map {|i| [i.titleize, i]}
end
end
并在视图中使用
<%= f.input :expense_type, collection: whitelisted_expense_types, as: :radio_buttons %>