Ruby on rails activeadmin下拉列表使用帮助器方法将值传递给集合
如何使用show页面上的helper方法来显示apple而不是op1 此示例使用帮助器方法将值传递给集合。Ruby on rails activeadmin下拉列表使用帮助器方法将值传递给集合,ruby-on-rails,activeadmin,Ruby On Rails,Activeadmin,如何使用show页面上的helper方法来显示apple而不是op1 此示例使用帮助器方法将值传递给集合。 “op1”保存在db中,“apple”显示在下拉列表中 app/admin/example.rb form do f.input :name, :as => :select, :collection => test_method end show do attributes_table_for example do row("Name") { ex
“op1”保存在db中,“apple”显示在下拉列表中 app/admin/example.rb
form do
f.input :name, :as => :select, :collection => test_method
end
show do
attributes_table_for example do
row("Name") { example.name }
end
end
def test_method
hash = {"apple" => "op1",
"orange" => "op2",
"berry" => "op3"}
hash
end
示例\u helper.rb
form do
f.input :name, :as => :select, :collection => test_method
end
show do
attributes_table_for example do
row("Name") { example.name }
end
end
def test_method
hash = {"apple" => "op1",
"orange" => "op2",
"berry" => "op3"}
hash
end
我将把hash变量作为常量放在ActiveRecord模型中。然后您可以使用:
attributes_table_for example do
row("Name") { MyModel::HASH_CONSTANT[example.name.to_sym] }
end
散列应该是
op1:“apple”
对,而不是“apple”=>“op1”
如何将散列转换为下拉列表的符号collection=>MyModel::HASH_CONSTANTMyModel::HASH_CONSTANT.map{| key,value |[value,key]}