如何在 rails 视图中遍历数组?

How can I iterate through an array in rails view?

提问人:Carlos Morales 提问时间:4/11/2014 最后编辑:Rajesh OmanakuttanCarlos Morales 更新时间:8/30/2017 访问量:28452

问:

我在 MySql 中进行了查询,但在 Rails 和 mysql2 gem 中工作。

信息如下:

http://sqlfiddle.com/#!2/9adb8/6

查询工作正常,没有问题,并显示以下结果:

 UNIT  V1  A1  N1   V2 A2  N2   V3  A3  N3   V4  A4  N4   V5  A5  N5
 LIFE  2   0   0    1   2  0    0   0   0     0   0   0   0    0   0
 ROB   0   1   0    0   1  2    0   0   0     0   0   0   0    0   0 

- 为 rails 2.3.8 安装了 mysql2 gem

gem install mysql2 -v0.2.6

·创建控制器:

class PolicyController < ApplicationController

   def index
      @result =  ActiveRecord::Base.connection.execute("select distinct @sql := concat('SELECT pb.name as unit,',group_concat(concat('SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,',SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,',SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),' FROM cia_ensures ce LEFT JOIN policies p on ce.id = p.cia_ensure_id INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id  INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id AND ca.id=1  Group by p.policy_business_unit_id') from cia_ensures where id in(1,2,3,4,5);")
      @result2 = ActiveRecord::Base.connection.execute("prepare stmt from @sql;")
      @result3 = ActiveRecord::Base.connection.execute("execute stmt;")
   end

end

日志如下:

SQL (0.9ms)   select distinct @sql := concat('SELECT pb.name as unit,',group_concat(concat('SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,',SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,',SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),' FROM cia_ensures ce LEFT JOIN policies p on ce.id = p.cia_ensure_id INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id AND ca.id=1 Group by p.policy_business_unit_id') from cia_ensures where id in(1,2,3,4,5);
SQL (0.9ms)   prepare stmt from @sql;
SQL (0.2ms)   execute stmt;

这是视图(工作正常且没有问题,但似乎太长,写了几次相同的代码)

<table>
  <% @result3.each do |policy| %>
  <tr>
      <td><%= policy[0] %></td>
      <td><%= policy[1] %></td>
      <td><%= policy[2] %></td>
      <td><%= policy[3] %></td>
      <td><%= policy[4] %></td>
      <td><%= policy[5] %></td>
      ...
  </tr>
  <%end%> 
</table>

我尝试使用 inspect,但它在一个 td 中显示所有信息,而不是在每个 td 上显示所有信息:

<% @result3.each do |policy| %>
<tr>      
 <td align="center"><%= policy.inspect %></td>
</tr>
<%end%> 

我怎样才能在不写很多行的情况下展示所有这些?

是否可以在一行中做到这一点?不写 <%= policy[#NUMBER] %>

请有人可以帮我解决这个问题吗?

我将不胜感激。

Ruby-on-Rails 红宝石 Ruby-on-Rails-2 mysql2

评论

0赞 Rajesh Omanakuttan 4/11/2014
你能只检查“@result3”并显示内容吗?
0赞 Carlos Morales 4/11/2014
是的,我试过了,但它在一个 td 中显示所有内容,而不是在每个 td 中显示
0赞 toniedzwiedz 4/11/2014
您不能像使用每个策略对象一样使用吗?each@result3
0赞 Carlos Morales 4/11/2014
是的,但是如果我有 100 列会发生什么?这是一个很长的代码

答:

18赞 Igor Pantović 4/11/2014 #1

嗯,可能误解了你的问题,因为它看起来很简单,你是想缩短这个吗?

改变:

<td><%= policy[0] %></td>
<td><%= policy[1] %></td>
<td><%= policy[2] %></td>
<td><%= policy[3] %></td>
<td><%= policy[4] %></td>
<td><%= policy[5] %></td>

自:

<% policy.each do |p| %>
  <td><%= p %></td>
<% end %>
2赞 toniedzwiedz 4/11/2014 #2

只需遍历每个对象中的每个集合即可。policy

<table>
  <% @result3.each do |policy| %>
      <tr>
          <%policy.each do |item| %>
              <td><%= item %></td>
          <%end%>
      </tr>
  <%end%> 
</table>
4赞 Rajdeep Singh 4/11/2014 #3

执行此操作

  <% @result3.each do |policy| %>
  <tr>
      <% policy.each { |p| raw "<td>#{p}</td>" } %>
  </tr>
  <%end%>