简体   繁体   中英

Rails: Loop hash on the hash

I want to loop hash. This is my hash:

@mcampaign_facturations=
{:"1"=>{:name=>"metacamapagne2", :name_comp=>"Pole Emploi", :title=>"Enquête HSOT DE A / Enquête HSOT DE B / Enquête HSOT Entreprises / Enquête HSOT DE A"}, :"3"=>{:name=>"metacamapagne1", :name_comp=>"GDF SUEZ", :title=>"Enquête SMS Esprit Services Energie Offres Grises / Enquête SMS Esprit Services Mensualisation"}, :"4"=>{:name=>"metacamapagne3", :name_comp=>"Virgin Mobile - Baromètre SatCli", :title=>"Baromètre SatCli"}, :"5"=>{:name=>"metacamapagne6", :name_comp=>"Ascom", :title=>"Campagne Satisfaction ICOYOTE Suresnes"}, :"10"=>{:name=>"metacamapagne2", :name_comp=>"Ascom", :title=>"Campagne Satisfaction Admin Suresnes"}, :"14"=>{:name=>"metacamapagne12", :name_comp=>"Pro-AM Relation Client", :title=>"PRO-AM de la Relation Client"}, :"15"=>{:name=>"metacamapagne12", :name_comp=>"monabanq.", :title=>"Campagne Satisfaction"}, :"16"=>{:name=>"test", :name_comp=>"GDF SUEZ", :title=>"Enquête SMS Esprit Services Diagnostic Economie / Enquête SMS Esprit Services Appel Entrant"}}

I want to display on the table like so

%table.tab{:border => 0, :cellspacing =>0, :cellpadding => '10px', :width => '100%'}
    %thead
      %tr
        %th{:align => "center"} #{t 'Name'}
        %th{:align => "center"} #{t 'Company'}
        %th{:align => "center"} #{t 'Campaigns'}


    - @mcampaign_facturations.each do |key, value|
      - value.each do |k, v|
        %tr.df{:class=>cycle('odd','even')}
          %td.title{:align => "center"}= v[:name]
          %td.title{:align => "center"}= v[:comp_name]

          %td.title{:align => "center"}= v[:title]

You have too many each loops - it's enough if you iterate through mcampaign_faacturations :

    - @mcampaign_facturations.each do |_, v|
      %tr.df{:class=>cycle('odd','even')}
      %td.title{:align => "center"}= v[:name]
      %td.title{:align => "center"}= v[:comp_name]

      %td.title{:align => "center"}= v[:title]

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