在rails项目中显示下拉列表

时间:2014-04-23 12:57:27

标签: ruby-on-rails

我在我的rails项目中有这个常量

   ID_METHODS = {      
          :category => [
              "name 1",
              "name 2",
              "name 3"
              "name 4",
              "name 5",
              "name 6"
              "name 7",
              "name 8",
              "name 9"
          ]
      }

使用以下代码

在下拉列表中显示它们
<%= f.select :id_methods, CategoryConstants::ID_METHODS[:staff].collect { |i| [i, i] }, {include_blank: "Select"} -%>

结果输出

name 1
name 2
name 3
name 4
name 5
name 6
name 7
name 8
name 9

但我需要在下拉列表中输出

name 1
 name 2
 name 3
  name 4
  name 5
name 6
name 7 
 name 8
 name 9

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我认为你需要grouped_options_for_select

实施例

 collection = ["North America",[["United States","US"],["Canada","CA"]]]

然后

<%= select_tag(::id_methods, grouped_options_for_select(collection, selected_key = " ", prompt = nil)) %>

请详细说明click