Topic: problem with creating a table layout with dynamic data...

I am trying to create a table in the following form based on data I am pulling from a db.

Monday, Oct 6              Wednesday, Oct 8           Monday, Oct 13
Red vs. Green              Blue vs. Yellow            Red vs. Green
Blue vs. Yellow            Red vs. Green              Blue vs. Yellow

Wednesday, Oct 15          Monday, Oct 20             Wednesday, Oct 22
Red vs. Green              Blue vs. Yellow            Red vs. Green
Blue vs. Yellow            Red vs. Green              Blue vs. Yellow

I can pull all the data from the db, I am having trouble looping through and displaying it in some form similar to this.

I've spent hours, ending up with messy, convoluted code, and wonder if anyone has any ideas or knows of rails conventions that would make this easier to handle.

Thanks!

Re: problem with creating a table layout with dynamic data...

Take a look at the in_groups_of method here:

http://railsforum.com/viewtopic.php?id=2731

HTH!

Re: problem with creating a table layout with dynamic data...

Thanks, I had no clue that function existed!

I am getting the following error when I run the code below, but can't figure out it's meaning...

Error:
compile error
./script/../config/../app/views/schedules/list.rhtml:39: syntax error
_erbout.concat "       ";  end ; _erbout.concat "\n"
                                ^

Extracted source (around line #39):

36:                 <% end %>
37:              <% end %>
38:            </tr>     
39:        <% end %>
40:     </table>
41:   </div>



#list view
    <h3>Listing schedules for <%= @league_info.league_description %></h3>
    <table>
       <% current_date = 0 %>
       <% for @schedules.in_groups_of(12) do |schedules| %>         
           <% if current_date != schedule.date %>         
              <tr><td width="187" height="13" bgcolor="#A3B36F" colspan="7"><strong><%= (Time.at(schedule.date)).strftime("%A, %B %d") %></strong></td></tr>
              <% current_date = schedule.date %>
           <% end %>
           <tr>
             <% for schedule in schedules %>
                <td width="72" height="11"><%= schedule.home_team.team_name %></td>
                <td width="20" height="11">vs.</td>
                <td width="72" height="11"><%= schedule.away_team.team_name %></td>
                <td width="10" height="11"><%= schedule.field.field_name %></td>
               
                <% if logged_in? && current_user.permissions >= 5 %>
                    <td><%= link_to 'Show', :action => 'show', :id => schedule.id, :league_id => @league_info.id %></td>
                    <td><%= link_to 'Edit', :action => 'edit', :id => schedule.id, :league_id => @league_info.id %></td>
                    <td><%= link_to 'Destroy', { :action => 'destroy', :id => schedule.id }, :confirm => 'Are you sure?', :post => true %></td>
                <% end %>
             <% end %>
           </tr>     
       <% end %>
    </table>

#list method in Schedules controller
def list
    @leagues = League.find(:all)
    @league_info = League.find(params[:id])
    @schedules = Schedule.find(:all, :conditions => [ "league_id = ?", @league_info.id,],    :order => "date")
end

I can get the schedules to print in blocks, but I want to display those three blocks across...

Re: problem with creating a table layout with dynamic data...

I'm not sure what the problem is. You may want to try commenting out various sections of the view to see what is causing the error.

It's fairly strange you are calling "schedule.date" at the beginning of the loop where there is no "schedule" set. Perhaps this is part of the problem (though I would expect a different error message).

I think there's a better way to what you are trying to do here. Fix the error message first then I'll go into detail.

Railscasts - Free Ruby on Rails Screencasts

Re: problem with creating a table layout with dynamic data...

Here is my code before I added the in_groups_of method. This works, it just places the output straight down the page. I want to format it into three columns...

I will play with the in_groups_of some more, though I commented out various parts and keep getting the same error no matter what... sad

#view (controller code is same as above post)

<table>
       <% current_date = 0 %>
       <% for schedule in @schedules %>         

                <% if current_date != schedule.date %>         
                  <tr><td width="187" height="13" bgcolor="#A3B36F" colspan="7"><strong><%= (Time.at(schedule.date)).strftime("%A, %B %d") %></strong></td></tr>
                  <% current_date = schedule.date %>
               <% end %>
               <tr>         
                    <td width="72" height="11"><%= schedule.home_team.team_name %></td>
                    <td width="20" height="11">vs.</td>
                    <td width="72" height="11"><%= schedule.away_team.team_name %></td>
                    <td width="10" height="11"><%= schedule.field.field_name %></td>
                   
                    <% if logged_in? && current_user.permissions >= 5 %>
                        <td><%= link_to 'Show', :action => 'show', :id => schedule.id, :league_id => @league_info.id %></td>
                        <td><%= link_to 'Edit', :action => 'edit', :id => schedule.id, :league_id => @league_info.id %></td>
                        <td><%= link_to 'Destroy', { :action => 'destroy', :id => schedule.id }, :confirm => 'Are you sure?', :post => true %></td>
                    <% end %>
               </tr>     

       <% end %>
    </table>

Re: problem with creating a table layout with dynamic data...

Ok I managed to fix the in_groups_of - kind of...
I had an extra 'for' where I didn't need it.

Now I get a nil object error (nil.date for schedule.date)

Here is the code - I am not familiar with the in_groups_of method, perhaps I am missing something here...

[code = ruby]
#view
<h3>Listing schedules for <%= @league_info.league_description %></h3>
    <table>
       <% current_date = 0 %>
       <% @schedules.in_groups_of(12) do |schedules| %>         
            <% schedules.each do |schedule| %>
                <% if current_date != schedule.date %>         
                  <tr><td width="187" height="13" bgcolor="#A3B36F" colspan="7"><strong><%= (Time.at(schedule.date)).strftime("%A, %B %d") %></strong></td></tr>
                  <% current_date = schedule.date %>
               <% end %>
               <tr>         
                    <td width="72" height="11"><%= schedule.home_team.team_name %></td>
                    <td width="20" height="11">vs.</td>
                    <td width="72" height="11"><%= schedule.away_team.team_name %></td>
                    <td width="10" height="11"><%= schedule.field.field_name %></td>
                   
                    <% if logged_in? && current_user.permissions >= 5 %>
                        <td><%= link_to 'Show', :action => 'show', :id => schedule.id, :league_id => @league_info.id %></td>
                        <td><%= link_to 'Edit', :action => 'edit', :id => schedule.id, :league_id => @league_info.id %></td>
                        <td><%= link_to 'Destroy', { :action => 'destroy', :id => schedule.id }, :confirm => 'Are you sure?', :post => true %></td>
                    <% end %>       
               </tr>     
          <% end %>
      <% end %>
   </table>
[/code]

Re: problem with creating a table layout with dynamic data...

Oh, the "for" in front of the in_groups_of method is the problem. Can't believe I missed that. It should be this:

<% @schedules.in_groups_of(12) do |schedules| %>

Railscasts - Free Ruby on Rails Screencasts

Re: problem with creating a table layout with dynamic data...

yeah those sneaky 'for' statements...

anyone have an idea of why I would be getting a nil object error?

It works when I don't use the in_groups_of method - i.e. when I interate straight through the @schedules object (for schedule in @schedules)

Re: problem with creating a table layout with dynamic data...

See this post for an explanation on why you are getting an error. A simple fix is to compact the array before looping:

       <% @schedules.in_groups_of(12) do |schedules| %>          
          <% schedules.compact.each do |schedule| %>
             ....
          <% end %>
      <% end %>

Railscasts - Free Ruby on Rails Screencasts