在sorted

时间:2016-08-18 17:01:33

标签: ruby-on-rails

目前,我有这样的哈希:

@lg = {
  "Latin East Group" => [
    {:id => 2, :name => "HONGKONG"},
    {:id => 3, :name => "NINGBO, ZHEJIANG"},
    {:id => 4, :name => "SINGAPORE"},
    {:id => 5, :name => "LARCHMONT, NY"},
    {:id => 6, :name => "CHICAGO, IL"},
    {:id => 7, :name => "HAIPHONG"},
    {:id => 8, :name => "DANANG"},
    {:id => 9, :name => "HANOI"},
    {:id => 10, :name => "MARSEILLE"},
    {:id => 11, :name => "LONDON"},
    {:id => 12, :name => "EDINBURGH"},
    {:id => 13, :name => "AMSTERDAM"},
    {:id => 14, :name => "HOCHIMINH"},
    {:id => 15, :name => "SHANGHAI"}
  ],
  "Latin West Group" => [],
  "Others" => [
    {:id => 16, :name => "Brazil" },
    {:id => 17, :name => "Mexico" },
    {:id => 18, :name => "Columbia"}
  ]
}

现在,我正在使用select2和我的表单,我想从该哈希实例变量创建一个下拉菜单。我希望哈希的键将是optgroups,而选项值将是新加坡,巴西等哈希的名称...因此,我想知道它的正确语法是什么。目前,这是我的代码:

_form_body.haml

 %div.col-md-8
    = f.grouped_collection_select :origin_locations, @lg, @lg.keys, @lg.values, {:selected => f.options[:origin_locations]}, {class: 'form-control select2-multiple origin_locations', :multiple => true}

pricing_histories_controller.rb

def load_location_groups
  @lg = {}
  location_groups = LocationGroup.all.includes(:locations).each { |group|
    @lg[group.name]= group.locations.map{|l| {id: l.id,name: l.name}}
    }
    # location_groups.each_with_index do |location_group, index|
    arr = Location.where("id NOT IN (SELECT DISTINCT(location_id) FROM location_group_assignments)").pluck(:id,:name)
    @location_groups = {}
    @lg["Others"] = arr.map{ |e| {id: e.first, name: e.last}}
end

我会收到错误:

  

ActionView :: Template :: Error([" Latin East Group"," Latin West Group",   "其他"]不是符号也不是字符串)

所以,我想知道我在这里做错了什么。任何建议,将不胜感激。谢谢你,祝你有个愉快的一天。

0 个答案:

没有答案