[英]How to add “0,” after each loop in Ruby
How to add "0," after each loop in Ruby (At the data: part of the series) 如何在Ruby中的每个循环之后添加“ 0”(在数据处:系列的一部分)
So first loop would
show only the value
next loop 0,value
next loop 0,0,value
next loop 0,0,0,value
etc..
series: [
<% @prot = ProjectTask.where("project_id = ? AND taskType = ?" ,@project.id, "Pre-Sales")%>
<% @prot.each do |prt| %>
<% hoursSum = 0 %>
{
name: '<%= prt.task_name%>',
data: [(here after each loop of @prot i want to add "0," here)<% @taskHours = Effort.where(:project_task_id => prt.id) %>
<% @taskHours.each do |th| %>
<% hoursSum = hoursSum + th.hours %>
<% end %>
<%= hoursSum%>
]
},<% end %>
<% @prot.each_with_index do |index, prt| %>
...
<%= "0," * index %>
...
<% end %>
This logic seems too complex to be in a view - I would migrate it to a helper function that builds up the data array and call that instead. 这种逻辑似乎太复杂了,无法在视图中显示-我将其迁移到构建数据数组的辅助函数中,然后调用该函数。
I think you'll find it a lot easier to do what you want then without having to deal with all the clutter of the erb tags etc. There's lots of ways you could do it - Yossi's suggestion of using each_with_index
is a perfectly good one. 我认为您会发现,无需处理所有erb标记等杂乱无章的事情就容易多了。您可以通过多种方式进行操作each_with_index
建议使用each_with_index
是一个非常好的方法。
Two little things though - I would advise against shortened names for stuff like @prot
- just call it @project_task
. 不过有两件事-我建议不要使用@prot
类的缩写名-只需将其命名为@project_task
。 It's more readable and you can guarantee you call it the same thing throughout your code (instead of having some places where you call it @prot
, others where it's @ptask
etc) which will save you more time than you lose typing a longer name, I promise you. 它更具可读性,您可以保证在整个代码中都使用相同的名称(而不是在某些地方将其@prot
,而在其他地方则将其@ptask
等)将为您节省更多的时间,而不会丢失键入更长的名称的情况,我答应你。
Also - you use camelCase for some of your variables - I would advise sticking with the Ruby convention of snake_case. 另外-您对某些变量使用camelCase-我建议您坚持使用snake_case的Ruby约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.