Rails 有没有其他方法可以做这个循环?干燥 [英] Rails is there any other way of doing this loop? DRY

查看:36
本文介绍了Rails 有没有其他方法可以做这个循环?干燥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 以保持干燥而闻名.

Rails is know for keeping things DRY.

我要创建一个这样的表格:http://www.duoh.com/csstutorials/tablesv2/

I am going to create a table like this: http://www.duoh.com/csstutorials/tablesv2/

有没有其他方法可以重复循环?

Is there any other way of doing repeated loops?

<tbody>
    <tr>
        <th class="column1" scope="row">Data usage</th>

<%= @something.each do |info| %>
        <td><%= info.name %></td>
<% end %>
    </tr>   
     <tr class="odd">
        <th class="column1" scope="row">Opslag Capaciteit</th>

<%= @something.each do |info| %>
<td><%= info.price %></td>
<% end %>
    </tr>   
    </tbody>

推荐答案

抽象功能正是 ruby​​ 的块/产量结构的目的.

Abstracting functionality is precisely the purpose of ruby's block/yield structure.

在助手中:

def tds list
  list.map do |item|
    content_tag :td, yield(item)
  end.join("\n")
end

那么在你看来:

<%= tds @something {|i| i.name } %>

<!-- other stuff -->

<%= tds @something {|i| i.price } %>

这篇关于Rails 有没有其他方法可以做这个循环?干燥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆