Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails activeadmin下拉列表使用帮助器方法将值传递给集合_Ruby On Rails_Activeadmin - Fatal编程技术网

Ruby on rails activeadmin下拉列表使用帮助器方法将值传递给集合

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

如何使用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") { 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]}