【问题标题】:Rails auto increment printRails 自动增量打印
【发布时间】:2013-08-05 08:40:15
【问题描述】:

我有以下一段代码,它打印分隔列中的项目列表。

- if @product.type_id == 2
      %th.order_invoice_cell.each.count
        #{l(:item)}
      - @product.products.each do |p|
        %th.order_invoice_cell
          #{l(:item)}

但是所有的列都被命名为 item、item、item。现在,如果我想提及 item1 item2 item3 等数字。我该怎么做。

【问题讨论】:

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


    【解决方案1】:

    我不知道,你的 #{l(:item)} 在做什么。但你可以使用这个:

    - if @product.type_id == 2
          %th.order_invoice_cell.each.count
            #{l(:item)}
          - @product.products.each_with_index do |p, index|
            %th.order_invoice_cell
              = "item #{index + 1}"
    

    它会给你第 1 项,第 2 项等等...谢谢

    【讨论】:

      【解决方案2】:

      请改用each_with_index。所以你可以做@product.products.each_with_index do |p,index|

      【讨论】:

      • 你能告诉我如何以及在哪里可以在我的代码中准确地使用它。因为我是新来的haml ..
      • 我已经用那个替换了第四行,但它不起作用。
      猜你喜欢
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 2012-02-08
      • 2014-12-12
      • 2010-10-27
      • 1970-01-01
      • 2010-11-22
      • 2013-02-10
      相关资源
      最近更新 更多