2

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

2
  • how did @stuff get data ? Commented Feb 13, 2013 at 12:21
  • I looked at the following stack question - stackoverflow.com/questions/2128663/… and am trying to replicate this but getting some data from my HospitalBooking table Commented Feb 13, 2013 at 12:51

1 Answer 1

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>                   
Sign up to request clarification or add additional context in comments.

4 Comments

That is precisely what I want to do.
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
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.
I have updated my question to include a full in depth explanation of what I am trying to achieve

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.