Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails每个循环逻辑_Ruby On Rails_Foreach - Fatal编程技术网

Ruby on rails Rails每个循环逻辑

Ruby on rails Rails每个循环逻辑,ruby-on-rails,foreach,Ruby On Rails,Foreach,我的每个循环都有问题。打印programdetail.name和programdetail.bodypart的行不打印该值。另外,你知道如何使这个循环更有效一点吗?我想打印前两个带有“奇”类的项目,另外两个带有非类的项目。诸如此类 <% @counter = 0 %> <% @program.programdetails.each do |programdetail| %> <% @counter = @counter + 1 %> <%

我的每个循环都有问题。打印programdetail.name和programdetail.bodypart的行不打印该值。另外,你知道如何使这个循环更有效一点吗?我想打印前两个带有“奇”类的项目,另外两个带有非类的项目。诸如此类

<% @counter = 0 %>
<% @program.programdetails.each do |programdetail| %>
    <% @counter = @counter + 1 %>
    <% @counter = @counter % 3 %>
    <% if (@counter == 0)
      @counter -= 1
    end %>
    <%= '<h3 class="odd"><span class="moduleLabel"> #{programdetail.name}</span><span class="moduleDescription">#{programdetail.bodypart}</span></h3>' if @counter != 0 %>
    <%= '<h3><span class="moduleLabel">#{programdetail.name}</span><span class="moduleDescription">#{programdetail.bodypart}</span></h3>' if @counter != 0 %>

<% end %>

循环
如果您想要奇偶组合,或在集合上使用,则助手可以工作:

<% @program.programdetails.each do |programdetail| %>

  <h3 class="<%= cycle("odd", "odd", "", "") %>
    <span class="moduleLabel"><%= programdetail.name %></span>
    <span class="moduleDescription"><%= programdetail.bodypart %></span>
  </h3>

<% end %>


cycle
helper可以工作,如果您想要奇偶组合,或者在集合上:

<% @program.programdetails.each do |programdetail| %>

  <h3 class="<%= cycle("odd", "odd", "", "") %>
    <span class="moduleLabel"><%= programdetail.name %></span>
    <span class="moduleDescription"><%= programdetail.bodypart %></span>
  </h3>

<% end %>


您的解决方案总是将“奇数”类打印到h3标记。另外,由于第一个解决方案打印3和4个元素的class=”“,CSS变得非常糟糕。那么,是否有一种解决方案可以不为每3个和第4个元素打印class=“”?
@Yagiz:也更正了第二个代码。前面的一个在逻辑上是错误的。您的解决方案总是将“奇数”类打印到h3标记。另外,因为第一个解决方案为3和4个元素打印class=“”,CSS变得非常糟糕。那么,是否有一种解决方案可以不为每3个和第4个元素打印class=“”?
@Yagiz:也更正了第二个代码。前面的一个在逻辑上是错误的。我建议您使用我很惊讶它会打印任何值,因为您在单引号字符串中使用字符串插值。这应该只适用于带双引号的字符串。我建议您使用它打印任何值,因为您在单引号的字符串中使用字符串插值。这应该只在dobule引用的字符串中起作用。