Home > OS >  Convert a list of combination arrays to a HTML table?
Convert a list of combination arrays to a HTML table?

Time:02-06

I think my question is a bit of a 2-parter. I'd like to be able to show a list of combinations in a HTML table. However, I think my problem is within my combinations I have not defined what each part of the combination is called.

Here's my combination 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}}]

To clarify, each driver and team have an associated points and price value. The idea is to show all combinations of 1 team and 3 drivers that are ranked by highest points whilst being below a price of 13.5. The combination consists of 5 parts: Team, 1 Driver that has had it's points value doubled, a combination of 2 other drivers, a total price of the sum of team and 3 drivers, and total points of the team and 3 drivers including the doubled driver.

This is the last section of code in controller which is creating the final output:

    # 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

I have a table in View that I am trying to map each value to, this isn't throwing errors but just isn't working:

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

The reason this is a two parter is because I believe the problem is that in my code I haven't defined what ':points', ':price' is etc. But I also am not sure how to. Is it a case of converting the combo to a new hash and somehow defining keys/values that I can then place in the table?

CodePudding user response:

Let's say this is your output variable

`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}}]`

and after you do each (if you print mycombo), you will get

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

So directly calling the [:price] etc won't work, you need to use output["Mer lec sai ham"][:price]

Note: You can try the debug mode or some sort of debugging using binding.pry (pry gem) to check if the syntax output is as expected

  • Related