加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 每个循环中的自动递增值

发布时间:2020-12-17 02:47:39 所属栏目:百科 来源:网络整理
导读:在代码中: % @offers.each do |offer|% %= offer.percentageOff% b% OFF on order of/b %= image_tag('1407951522',:size='10x10',:alt='Rs.')% %= offer.amountforDiscount% %= button_to 'Delete',{:action='destroy',:id=offer.id},class: "" %% end %
在代码中:

<% @offers.each do |offer|%>
  <%= offer.percentageOff%> <b>% OFF on order of</b>
  <%= image_tag('1407951522',:size=>'10x10',:alt=>'Rs.')%>
  <%= offer.amountforDiscount%>
  <%= button_to 'Delete',{:action=>'destroy',:id=>offer.id},class: "" %>
<% end %>

我是铁杆新手.我想在编号列表中显示所有商品,我不能使用数据库表,因为id不按顺序排列.例如:

> 2000卢比> 30%OFF
> 1000卢比> 13%折扣

我如何实现这一目标?

解决方法

最简单的方法,如果你不想使用each_with_index那么你可以定义变量@count = 0并显示,当循环发生它将增加1

<% @count = 0 %>
<% @offers.each do |offer|%> 
<%= @count += 1 %> #I guess you want this to show Sr.No
...... # your code
<% end %>

each_with_index方式:

<% @offers.each_with_index do |offer,index|%> 
 <%= index + 1 %> # index starts from 0,so added 1 to start numbering from 1
  ...... # your code
<% end %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读