1

I want to display some data from a table and display it in the table cells. I have the following.

Controller

@data = HospitalBooking.where(:created_at => @date_range)
@stuff = [] 
@stuff[0] = [] 

index = 0 

@stuffs.each do |stuff|
  @rotated[0][index] = stuff.detail0
  index += 1
end

Not sure if I have gone about this the correct way also how would I display it in my view

Deej
  • 5,334
  • 12
  • 44
  • 68
  • how did @stuff get data ? – Abhay Kumar Feb 13 '13 at 12:21
  • I looked at the following stack question - http://stackoverflow.com/questions/2128663/display-data-in-columns-not-rows-using-ruby-on-rails and am trying to replicate this but getting some data from my `HospitalBooking` table – Deej Feb 13 '13 at 12:51

1 Answers1

7

If I understood it right, you want to display the result of a query in a html table.

Here is your query, in your controller:

@data = HospitalBooking.where(:created_at => @date_range)

In your view

<table>                         
  <tr>                          
    <th>field1</th>
    <th>field2</th>
  </tr>
<% @data.each do |data| %>       #start loop
  <tr>
    <td><%= data.field1 %></td>  #column field1 in your database
    <td><%= data.field2 %></td>  #column field2 in your database
  </tr>
<% end %>                        #end loop
</table>                   
gabrielhilal
  • 10,660
  • 6
  • 54
  • 81
  • That is precisely what I want to do. – Deej Feb 13 '13 at 13:13
  • However what I am trying to do is get this data from my hospital booking table to appear in the table cells. I have updated my question further – Deej Feb 13 '13 at 13:20
  • if you want to display all data from that table, you can just change the query to: `@data = HospitalBooking.all` . and change field1 and field2 by the columns name. – gabrielhilal Feb 13 '13 at 13:22
  • I have updated my question to include a full in depth explanation of what I am trying to achieve – Deej Feb 13 '13 at 13:31