each_with_index无法正常工作

时间:2013-02-15 11:34:23

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 haml

我希望每个循环的第一个元素都是“活动的”并用活动的css类包装它。我尝试了这个,但现在每个项目都是活跃的。我做错了什么?

#myCarousel.carousel.slide
  .carousel-inner
    - @cities.each do |city|
      - city.activities.find_all_by_homepage_city(true).each do |b|
        - b.attachments.each_with_index do |a, index|
          - if index=0 
            .active.item
              = link_to(image_tag(a.file.url, :height =>"325px", :width =>"650px"))
              .carousel-caption
               %h4 yep 1
          - else 
            .item
              = link_to(image_tag(a.file.url, :height =>"325px", :width =>"650px"))
              .carousel-caption
                %h4 yep 2 

Thanks..remco

1 个答案:

答案 0 :(得分:4)

您输错了,if index == 0不是if index = 0

无论如何,如果我想做你正在做的事情,我会做以下

#myCarousel.carousel.slide
  .carousel-inner
    - @cities.each do |city|
      - city.activities.find_all_by_homepage_city(true).each do |b|
        - b.attachments.each_with_index do |a, index|
          %div{ :class => "#{index == 0? 'active item' : 'item'}" }
            = link_to(image_tag(a.file.url, :height =>"325px", :width =>"650px"))
            .carousel-caption
              %h4
                = index == 0 ? 'yep 1' : 'yep 2'