Ruby-on-rails 如何在 Rails 视图中遍历数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23004125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:32:23  来源:igfitidea点击:

How can I iterate through an array in rails view?

ruby-on-railsrubyruby-on-rails-2mysql2

提问by Carlos Morales

I did a query in MySql but is working in Rails and mysql2 gem.

我在 MySql 中做了一个查询,但在 Rails 和 mysql2 gem 中工作。

Here is the information:

以下是信息:

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

The query is working fine without problems and showing this result:

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

 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 

-Installed mysql2 gem for rails 2.3.8

-为 rails 2.3.8 安装了 mysql2 gem

gem install mysql2 -v0.2.6

-Created the controller:

- 创建控制器:

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

Here is the log:

这是日志:

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;

Here is the view (is working fine and without problems but seems to be too long write several times the same code)

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

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

I tried to use inspect but it shows all the information in one td and not on each td:

我尝试使用检查,但它显示了一个 td 中的所有信息,而不是每个 td:

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

How can I do to show all this without writing lots of lines?

我怎样才能在不写很多行的情况下展示这一切?

Is it possible to make this in one line? without writing <%= policy[#NUMBER] %>

是否可以在一行中完成此操作?不写 <%= policy[#NUMBER] %>

Please somebody can help me with this?

请问有人可以帮我吗?

I will really appreciate it.

我会很感激的。

回答by Igor Pantovi?

Hmmm, might have missunderstood your question since it looks really simple, are you trying to shorten this?

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

Change:

改变:

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

To:

到:

<% policy.each do |p| %>
  <td><%= p %></td>
<% end %>

回答by Rajdeep Singh

Do this

做这个

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

回答by toniedzwiedz

Just iterate over each collection in each policyobject.

只需迭代每个policy对象中的每个集合。

<table>
  <% @result3.each do |policy| %>
      <tr>
          <%policy.each do |item| %>
              <td><%= item %></td>
          <%end%>
      </tr>
  <%end%> 
</table>