0

我正在使用带有集合的输入字段,该集合是从模型中的数组中提取的。这很好用,但我想为表中的实际列返回一个不同的值。我正在使用simple_form

模型

TASK_OPTIONS = %w(Detection Cloning Sequencing Primer_List Primer_Check)

看法

<%= f.input :primer_task, :collection => Primer3Batch::TASK_OPTIONS, :label => 'Task' %>

我可能想返回这样的东西:

{1 => 'Detection', 2 => 'Cloning'... etc

或这个:

{'AB' => 'Detection, 'C' => 'Cloning' ....

那就是:页面将显示检测、克隆等,但数据库列将存储1,2AB、C 我猜它可以用哈希来完成,但我不太清楚语法。

4

1 回答 1

0
a = []
%w(Detection Cloning Sequencing Primer_List Primer_Check).each.with_index(1) do |it,ind|
    a << [ind,it]
end
Hash[a]
# => {1=>"Detection",
#     2=>"Cloning",
#     3=>"Sequencing",
#     4=>"Primer_List",
#     5=>"Primer_Check"}

使用Enumerable#each_with_object

a = %w(Detection Cloning Sequencing Primer_List Primer_Check)
a.each_with_object({}) {|it,h| h[a.index(it) + 1 ] = it }
# => {1=>"Detection",
#     2=>"Cloning",
#     3=>"Sequencing",
#     4=>"Primer_List",
#     5=>"Primer_Check"}
于 2013-06-29T15:49:36.537 回答