In this second part of a two-part series, we'll finish creating reports that can be accessed via the Internet. This article is excerpted from chapter 5 of the book Practical Reporting with Ruby and Rails, written by David Berube (Apress; ISBN: 1590599330).
Building Reports Accessible from the Internet - Creating the Models for the Graphical Report (Page 4 of 5 )
This example will use four models: event (app/models/event.rb), game (app/models/game.rb), play (app/models/play.rb), and player (app/models/player.rb). Their code is shown in Listings 5-13 through 5-16.
Listing 5-13.Event Model (app/models/event.rb)
class Event < ActiveRecord::Base belongs_to :play end
Listing 5-14.Game Model (app/models/game.rb)
class Game < ActiveRecord::Base has_many :plays end
Listing 5-15.Play Model (app/models/play.rb)
class Play < ActiveRecord::Base belongs_to :game belongs_to :player end
Listing 5-16.Player Model (app/models/player.rb)
class Player < ActiveRecord::Base has_many :plays end
Creating the View for the Graphical Report
The final pieces are the views. Place the file shown in Listing 5-17 in app/views/home/index.html.erb .
Listing 5-17.View for the Team Performance Application (app/views/home/index.html.erb)
<h1>Team Performance Reporting</h1>
<div id="top"> <%=select 'player', 'id', [['Click here to select a player',""]] + @available_players.map { |p| [p.name, p.id] }, {:include_blank=>false} %> <%=select 'game', 'id', [['Click here to select a game',""]] + @available_games.map { |g| [g.name, g.id] }, {:include_blank=>false} %> </div> <div id="chart"> </div>
<script>
function show_report(){ $('chart').hide(); var player_id = $('player_id').value; var game_id = $('game_id').value if( player_id && game_id ) { new Ajax.Updater("chart", '/performance'+ '/' + $('game_id').value + '/' + $('player_id').value,