Rails question - Constructing a table by columns

Discussion in 'Web Design and Development' started by weatherwax, Jan 4, 2016.

  1. weatherwax macrumors member


    Jan 1, 2016
    Baltimore, MD
    Hi all!

    I'm currently working on creating a lightweight attendance app for my wife's small school. I have it set up with a "Users" model, which has many "Records", which are either "signed in", "signed out", "excused absence", or "unexcused absence".

    My wife has asked me to build a page that would resemble a report they currently produce on excel - the status of every student (names going across the top/x-axis) for every day within a range (running along the side/y-axis). I'm assuming that I'll need to loop through each User, and then loop through each User's Records to construct the view I want but I have two questions I'd love some suggestions on:

    1. How should I construct the table, considering it's organized with one user per column? Put each user loop in its own table? Construct in divs and then wrangle them into a grid with CSS?

    2. What's the best way normalize the table so that each row (running across Users) represents the same date? Each record comes with a created_at timestamp, and I've been using that to divide it into days in other areas of the app.
    Thanks in advance for your thoughts!
  2. nitromac macrumors 6502

    Jul 29, 2012
    Use embedded ruby (.html.erb, .html.haml, whatever you like) to run an each-loop through your list of users/records and within it, render the elements that you need. You could construct a <table> with each day being a <tr> and each user record being a <td>, but you would need to synchronize it so that each user has a record for each day (no gaps).

    You can use date parsing libraries (I think there is a built-in "Date" library in Rails) to get the date from the timestamp. You might want to rebuild the way the data looks in your controller to prepare it for the view. Instead of just spitting out a list of users and a list of records, try mapping it in the controller to make it easier to build in the view. Maybe construct an array of objects with a date, each of which contains a user ID and the corresponding record for that user on that date. Along with that, return an array of objects with user IDs and the user information you need (Name, etc). This way, in the view you can render the table easily by iterating over each date and aligning records by user ID.
  3. ForkHandles macrumors regular


    Jun 8, 2012
    Could you throw all of the data into an set of five equally sized arrays? Loop through each kids data and throw each piece of data into one of the five arrays. Then clock through the array and build a table from that.

Share This Page