简体   繁体   中英

How to iterate through hashes in an each_slice loop

On the line:

 partial_name = ((not group_data[:partial].nil?) && group_data[:partial]) || "basic_form_render";

I keep getting the error "can't convert Symbol into Integer". I figured that because I'm passing three instances of group_name and group_data at a time, so I added a variables in the .times look to keep track of each interation (0,1,2), like

partial_name = ((not group_data[times_iterator][:partial].nil?) && group_data[times_iterator][:partial]) || "basic_form_render";

where times iterator is initialized to 0 and increments in each passing of the 3.times loop. However, I still get the same error.

-form_data.each_with_index do |(category_name, cat_data), i|    
        -cat_data.each_slice(3).with_index do |(group_name, group_data), j|
          - counter+=1
          %div{:id => "page#{(counter)}", "data-role" => "page"}
            %h3{:style => "text-align:center;"}
              =category_name
            %hr{:color => "black", :size => "2"}/
            -3.times do 
              -partial_name = ((not group_data[:partial].nil?) && group_data[:partial]) || "basic_form_render";
              = render partial_name, :cat_no => (i+1), :group_no => (j+times_iterator), :group_name => group_name, :group_data => group_data
            =render "shared/form_footer"

I keep getting the error "can't convert Symbol into Integer"

It happens when you try to access the array element with symbols or strings, instead of an index.

group_data is an array, so group_data[:partial] will throw an error.

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