ruby-on-rails 是否将组合数组列表转换为HTML表格?

ryoqjall  于 2023-02-06  发布在  Ruby
关注(0)|答案(1)|浏览(287)

我想我的问题有点像两个部分。我希望能够在HTML表格中显示组合列表。然而,我想我的问题是在我的组合中我没有定义组合的每个部分被称为什么。
下面是我的组合输出:

[{"Mer lec sai ham"=>{:price=>13.1, :points=>53.4}}, {"Mer rus sai ham"=>{:price=>12.1, :points=>32.2}}, {"Fer rus sai ham"=>{:price=>13.1, :points=>31.4}}, {"Mcl rus sai ham"=>{:price=>13.5, :points=>14.9}}]

为了澄清,每个车手和车队都有一个相关的积分和价格值。这个想法是为了显示1支车队和3名车手的所有组合,这些组合按最高积分排名,同时价格低于13.5。组合包括5个部分:车队、1名积分值翻倍的车手、2名其他车手的组合、车队和3名车手之和的总价格、以及车队和3名车手(包括翻倍的车手)的总积分。
这是控制器中创建最终输出的最后一段代码:

# OUTPUT
    output = ordered.map do |c|
      { c.join(" ")=>{ price: add_up(c, team_price, driver_price),
             points: add_dbl(c, team_points, driver_points).round(2)} }
    end
    @combo = output

我在View中有一个表,我试图将每个值Map到该表,这不会引发错误,但不起作用:

<table class="table table-condensed">
   <thead>
      <tr>
         <th>Team</th>
         <th>Drivers</th>
         <th>Double</th>
         <th>Price</th>
         <th>Points</th>
      </tr>
   </thead>
   <tbody>
      <% @combo.each do |mycombo| %>
         <tr>
         <td><%= mycombo[:teams] %></td>
         <td><%= mycombo[:drivers] %></td>
         <td><%= mycombo[:double] %></td>
         <td><%= mycombo[:price] %></td>
         <td><%= mycombo[:points] %></td>
         </tr>
      <% end %>
   </tbody>
</table>

这是一个两部分的原因是因为我认为问题是在我的代码中我没有定义什么':points',':price'等,但我也不知道如何定义。这是一个转换组合到一个新的散列,并以某种方式定义键/值,然后我可以放置在表中的情况吗?

nszi6y05

nszi6y051#

假设这是您的输出变量

`output = [{"Mer lec sai ham"=>{:price=>13.1, :points=>53.4}}, {"Mer rus sai ham"=>{:price=>12.1, :points=>32.2}}, {"Fer rus sai ham"=>{:price=>13.1, :points=>31.4}}, {"Mcl rus sai ham"=>{:price=>13.5, :points=>14.9}}]`

并且在执行每个操作之后(如果打印mycombo),您将得到

{"Mer lec sai ham"=>{:price=>13.1, :points=>53.4}}

所以直接调用[:price]等是不行的,需要使用output["Mer lec sai ham"][:price]

注意:您可以尝试调试模式或使用binding.pry(pry gem)进行某种调试,以检查语法输出是否符合预期

相关问题