[英]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". 我不断收到错误消息“无法将Symbol转换为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 我发现,因为我一次传递了三个group_name和group_data实例,所以我在.times中添加了一个变量来跟踪每个交互(0,1,2),例如
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. 时间迭代器初始化为0,并在每次3.times循环通过时递增。 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" 我不断收到错误消息“无法将Symbol转换为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. group_data
是一个数组,因此group_data[:partial]
将引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.