如何显示字符串名称,但在活动管理员的下拉列表中将整数键保存到数据库? 我有这样的表格:
form do |f|
f.inputs "Status" do
f.input :status, as: :select, collection: App::STATUSES.keys
end
f.actions
end
产生这个并且工作正常:
但我需要它来产生这个:
这是产生上述图像的代码。它显示我想要的值,但它不会将密钥(0,1,2等)保存到db
form do |f|
f.inputs "Status" do
f.input :status, as: :select, collection: App::STATUSES.values
end
f.actions
end
我试过了,但它没有用:
form do |f|
f.inputs "Status" do
f.input :status, as: :select, collection: App::STATUSES.values, :label_method => :status_name
end
f.actions
end
这是我的模特:
IN_PROGRESS = 0 #default
SUBMITTED = 1
REVIEW = 2
PENDING = 3
APPROVED = 4
LIVE = 5
STATUSES = {
IN_PROGRESS => 'in progress',
SUBMITTED => 'submitted',
REVIEW => 'review',
PENDING => 'pending',
APPROVED => 'approved',
LIVE => 'live'
}
def status_name
STATUSES[status].to_s
end
status_name方法在我的应用程序中运行正常,在显示下拉列表时,由于某些原因它没有被调用。
答案 0 :(得分:1)
尝试将哈希用作集合(或反向哈希)
f.input :status, as: :select, collection: App::STATUSES.invert, :label_method => :status_name