rails在html表中组合来自2个不同来源的数据

时间:2014-06-05 23:37:56

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 ruby-on-rails-3.1

所以我从两个外部服务获取数据:

@list =  ExternalService.get_list
ids =  @list.map { |member| meber.id }

insurances = ExternalService2.get_insurance

# Get insurance information for each of the ids.
@coverages = insurances.map { |insurance| InsurancePresenter.new(insurance) }

/ In the view
- @list.each do |member|
  %tbody
    %tr
      %td= member.name
      %td= member.age

      / This is the part where I need to get information from the other service
      %td
        - @coverages.each |coverage|
          / method name from the presenter
          = coverage.name 

很明显,我的代码的最后一部分是不正确的,因为它会打印每行的所有coverage。

我想知道是否有一种快速的方法来组合2并在应用程序中显示它们。 如果是数据库请求,那将是一个简单的连接。

1 个答案:

答案 0 :(得分:0)

最简单的方法是为每个成员开始计数和迭代,并使用计数显示coverages数组中的每个coverage。抱歉,不知道HAML,但这是一般的想法。

/在视图中

count = -1
- @list.each do |member|
  %tbody
    %tr
      %td= member.name
      %td= member.age
      count+=1
      / This is the part where I need to get information from the other service
      %td
        - @coverages[count].name