简体   繁体   中英

How can I create a select list with grouped hash array of hashes instead of ActiveRecord in Rails?

I am trying to sort out how to present a grouped list from a hash with an array of hashes. I am obtaining information from another api source and converting JSON, then grouping by region.

@list ||= JSON.parse(@conn.get('/templates').body, symbolize_names: true)

@template_list = (@list.group_by {|k,v| k[:region]})

@template_list output looks like below.

{"EMEA"=>[{:id=>"1563", :url=>" https://website.com ", :name=>"TemplateNameA", :region=>"EMEA", :another_key=>"another value", :contain_count=> 0}, {:id=>"7819", :url=>" https://website.com ", :name=>"TemplateNameB", :region=>"EMEA", :another_key=>"another value", :contain_count=>0}], "Central"=>[{:id=>"421", :url=>" https://website.com ", :name=>"TemplateNameA", :region=>"Central", :another_key=>"another value", :contain_count=>0}], "East"=>[{:id=>"12", :url=>" https://website.com ", :name=>"TemplateName1", :region=>"East", :another_key=>"another value", :contain_count=>0}]}

After several iterations the closest I get to my desired outcome is <%= select_tag :template_id, grouped_options_for_select(@template_list) %>

Which results in pulling all key/value pairs instead of a nice short list. My desired output would look like the following.

<optgroup label="East">
  <option value="12">TemplateName1</option>
  <option value="321">TemplateName2</option>
</optgroup>
<optgroup label="EMEA">
  <option value="1563">TemplateNameA</option>
  <option value="7819">TemplateNameB</option>
  <option value="2367">TemplateNameC</option>
</optgroup>
<optgroup label="Central">
  <option value="421">TemplateNameA</option>
</optgroup>

grouped_options_for_select expects a Hash that looks like this:

{
  'group name 1': [
    [select name, select value],
    [select name, select value]
  ], 
  'group name 2': [
    [select name, select value],
    [select name, select value]
  ]
}

So we need only take your original data structure and convert it in to this format:

@data = {"EMEA"=>[{:id=>"1563", :url=>"https://website.com", :name=>"TemplateNameA", :region=>"EMEA", :another_key=>"another value", :contain_count=> 0}, {:id=>"7819", :url=>"https://website.com", :name=>"TemplateNameB", :region=>"EMEA", :another_key=>"another value", :contain_count=>0}], "Central"=>[{:id=>"421", :url=>"https://website.com", :name=>"TemplateNameA", :region=>"Central", :another_key=>"another value", :contain_count=>0}], "East"=>[{:id=>"12", :url=>"https://website.com", :name=>"TemplateName1", :region=>"East", :another_key=>"another value", :contain_count=>0}]}
@options = {}
@data.each do |name,data|
    @options[name] = data.collect do |x|
      [x[:name], x[:id]]
    end
end

This loops over all of your original Hash keys, then creates a new array for each array element, keeping only the name and the id.

Then pass @options to grouped_options_for_select and you should be in business!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM