【问题标题】:rails combining data from 2 different sources in an html tablerails 在 html 表中组合来自 2 个不同来源的数据
【发布时间】:2014-07-27 03:21:27
【问题描述】:

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

@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 

显然我的代码的最后一部分是不正确的,因为它会打印每一行的所有覆盖率。

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

【问题讨论】:

  • 覆盖范围和列表是否相关?如果在连接中很容易做到,那么这里可能很容易。只需要一个关联即可。
  • @Swards。它不相关。就像我提到的那样。它没有直接访问数据库。它是给我们返回 json 的外部服务。
  • 我明白了 - 这两个外部服务之间有什么关系吗?你怎么知道列表和覆盖范围是相关的?

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


【解决方案1】:

最简单的做法是开始计数并为每个成员进行迭代,并使用计数来显示 coverages 数组中的每个覆盖率。抱歉,我不知道 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 

【讨论】:

  • 语法 -@coverages[count].name 是否正确。我收到一个未定义的方法 [] 错误
  • 嗯,假设 Coverages 是一个包含 Coverages 对象的数组,那么您应该能够做到这一点。
猜你喜欢
  • 2021-08-07
  • 2015-10-31
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多