簡體   English   中英

索引在 Rails 中沒有按預期工作

[英]Indexing not working as expected in rails

在我的項目中,我想索引數據。

視圖代碼:

- @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 .......................................
        
  

我得到的結果是輸出獲得為 如果我將視圖上的代碼更改為:

- @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

結果如下: 輸出獲得為

我該如何解決這個問題?

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM