0

I have a Message model which has a create date. How can I make it so that I can display the date horizontally and the other attributes vertically.

So it would be something like this:

Date         1/1/11           2/1/11            3/1/11
Message      message1         message2          message3
Attr 1       attr1 val        ....              ...
Attr 2       attr2 val        ....              ......

Is there a plugin/gem that I could use in Rails, or I have to use some JavaScript library to do that?

1
  • It is just about HTML layout. You don't need Javascript here and you don't need any gems here. Commented May 9, 2011 at 16:24

1 Answer 1

4

in your controller prepare data

@messages = Message.select(:created_at, :message, :attr1, :attr2 ...)
@turned_messages = @messages.all.inject({}){ |h, c| c.attributes.each{ |k,v| h[k] ||= []; h[k] << v }; h }

Then in views:

<table>
  <% @turned_messages.each do |k, values| %>
    <tr>
      <td><%= k %></td>
      <% values.each do |v| %>
        <td><%= v %></td>
      <% end %>
    </tr>
  <% end %>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

That displays all the attributes in 1 line. Output looks like: attr1 attr1value attr2 attr2value. If I have multiple, it just puts everything in 1 line. I modified how I get @messages. This is what I have: @messages = Message.find_all_by_user_id(current_user.id) and I removed all in fron of inject. Could that be why my output messes up?
Actually it displays like: Message val1 val2 Attr1 attrval1 attrval2 Attr2 attrval2 attrval3....all in same line

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.