0

I am not a ruby developer. This is the first time I am looking into the code. I want to build a dynamic table for which I have managed below code. However, I am not able to display all the contents of the array except for the first and the last values. How do I display all the values?

Thanks for your help!!

<style>table, td, th{border:1px solid white;}td{padding:5px;}th{background-color:#E0E6EB;color:black;}</style>
<div>
<table border=2>
<tr>
  <th width="250px"><B><p style="text-align: center">Name</p></B></th>
<th width="120px"><B><p style="text-align: center">Number</p></B></th>
<th width="60px"><B><p style="text-align: center">Status</p></B></th>
<th width="155px"><B><p style="text-align: center">Product Type</p></B></th>
<th width="60px"><B><p style="text-align: center">Source</p></B></th>
</tr>
  <% tempTickets = @subject.PersonAccounts.sorted_by(field("title").in_descending_order) %>  
  <% cnt = tempTickets.length %>
  <% tempTickets.each do |ticket| %>
<div>
<tr>
  <td><%= ticket['perfinaccnt-accountname'].first %></td>
<td><%= ticket['perfinaccnt-accountnumber'].first %></td>
<td><%= ticket['perfinaccnt-accountstatus'].first %></td>
<td><%= ticket['perfinaccnt-producttype'].first %></td>
<td><%= ticket['perfinaccnt-accountsrcsystem'].first %></td>
</tr>
</div>
<div>
<tr>
  <td><%= ticket['perfinaccnt-accountname'].last %></td>
<td><%= ticket['perfinaccnt-accountnumber'].last %></td>
<td><%= ticket['perfinaccnt-accountstatus'].last %></td>
<td><%= ticket['perfinaccnt-producttype'].last %></td>
<td><%= ticket['perfinaccnt-accountsrcsystem'].last %></td>
</tr>
</div>

  <% end %>
</table>
</div> 
5
  • Do you know the attributes of the object? Commented Apr 21, 2015 at 22:07
  • I am not sure at all about the attributes. Since, I am able to display first and last elements of the array. how do I loop through it? Commented Apr 21, 2015 at 22:13
  • You're already looping through tempTickets using each method. Commented Apr 21, 2015 at 22:25
  • Is this what you looking for? stackoverflow.com/questions/310634/… Commented Apr 21, 2015 at 22:27
  • Based on the loop count, can I change the index value in the below code? ticket['perfinaccnt-accountname'].<index> Commented Apr 22, 2015 at 8:39

2 Answers 2

1

You could do it with three nested loops:

<%= tempTickets.each do |ticket|
    [
      'perfinaccnt-accountname',
      'perfinaccnt-accountnumber',
      'perfinaccnt-accountstatus',
      'perfinaccnt-producttype',
      'perfinaccnt-accountsrcsystem'
    ].each do |f|
        ticket[f].each do |tf|
            puts "<td>tf</td>";
        end if ticket[f]
    end 
end %>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the code - However, the code is printing below text outside the table. "[#<Entity:0x69b60a95 @entity=#<Java::ComVivisimoGelatoEntityResultset::SortedEntity:0x1abe9cdf>, @resolver=#<InstrumentationProxy:0x17e29ce0 @jmx_wrapper=#<JMXWrapper:0xeccd5d5d @mbean_store=#<MBeanStore:0x6b9e402e @mbean_store={}>, "
I can't really help you there without knowing what the data is. The text you are seeing is most likely due to one of the fields being a class thats not meant to be printed directly.
Then what could be the option to print a class?
Check the object.class would do that.
In my original code, ticket['perfinaccnt-accountname'].class is showing me the value as Array, and printing ticket['perfinaccnt-accountname'] gave me the list of values with comma separation enclosed within []
|
0

I'm not sure about the data structure here but you could check it by doing an #inspect to tempTickets and all the subsequent objects. From the looks of it "ticket['perfinaccnt-accountname']" is actually an array and as such you can just loop through it. Not the fastest way to do it but it should work to just do a nested loop.

<style>table, td, th{border:1px solid white;}td{padding:5px;}th{background-color:#E0E6EB;color:black;}</style>
<div>
<table border=2>
<tr>
  <th width="250px"><B><p style="text-align: center">Name</p></B></th>
<th width="120px"><B><p style="text-align: center">Number</p></B></th>
<th width="60px"><B><p style="text-align: center">Status</p></B></th>
<th width="155px"><B><p style="text-align: center">Product Type</p></B></th>
<th width="60px"><B><p style="text-align: center">Source</p></B></th>
</tr>
  <% tempTickets = @subject.PersonAccounts.sorted_by(field("title").in_descending_order) %>
  <% cnt = tempTickets.length %>
  <% tempTickets.each do |ticket| %>
  <div><tr>
    <%  ticket.each do |k,v|
          v.each do |col| %>
            <td><%= col %></td>
          <% end %>
        <% end %>
  </tr></div>
  <% end %>
</table>
</div> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.