如何在轨道的下拉字段中添加两个变量值?



我有一个下拉字段,因为我需要显示两个查询值。

a = table_first.first.name
b = table_second.all.collect{ |tc| [ "#{tc.name} [#{tc.number}]", tc.id ] }

使用options_for_select,我想在下拉字段中显示这两个值ab。 可能吗?

是的,您可以将第一个值插入到第二个数组中。

options_for_select(b.unshift(a))

应该以类似的方式返回

<option value="first">first</option>
<option value="1">Second [1]</option>

https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select

编辑

Rails 控制台中带有值的完整示例

[13] pry(main)> table_first = "first"
=> "first"
[14] pry(main)> table_second = [["Second [2]", 2]]
=> [["Second [2]", 2]]
[15] pry(main)> ApplicationController.helpers.options_for_select(table_second.unshift(table_first))
=> "<option value="first">first</option>n<option value="2">Second [2]</option>"

是的,这是可能的。

options_for_select(table_second.all.map { |obj| "#{obj.name}__#{obj.id}", obj.id })

最新更新