简体   繁体   中英

Indexing not working as expected in rails

In my project I want to index the data.

The code of views:

- @phones.each.with_index(1) do |(phone,no_of_duplicate),index|
  .row.py-2
     .col-2 = index
    - if no_of_duplicate.count > 1      
     .col-3 = phone
     .col-3 = no_of_duplicate.count
     .col-4 .......................................
        
  

I obtain the result as输出获得为 If I change the code on views as:

- @phones.each.with_index(1) do |(phone,no_of_duplicate),index|
 - if no_of_duplicate.count > 1
  .row.py-2
    .col-2 = index
    .col-3 = phone
    .col-3 = no_of_duplicate.count

The got the result as: 输出获得为

How can I solve this?

您可以在分配索引和渲染之前进行filter

@phones.filter { |(_,no_of_duplicate)| no_of_duplicate.count > 1 }.each <...>

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