Home arrow Ruby-on-Rails arrow Building Reports Accessible from the Internet

Building Reports Accessible from the Internet

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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
March 26, 2010
  1. · Building Reports Accessible from the Internet
  2. · Examining the We b Report Application
  3. · Graphical Reporting with Rails
  4. · Creating the Models for the Graphical Report
  5. · Examining the Graphical Reporting Application

print this article

Building Reports Accessible from the Internet
(Page 1 of 5 )

 Creating the Controller for the Web Report

The controller will respond to actions of the user by taking data from the model and presenting it via your views. The controller is named home, since you will have just one page. Create the app/controllers/home_controller.rb file with the code shown in Listing 5-6.

Listing 5-6. Home Controller for the Web Report (app/controllers/home_controller.rb)

class HomeController < ApplicationController
  def index
    @actors_today = []
    @actors_tomorrow = []
    Actor.find(:all).each do |actor|

      @actors_today << {:actor=>actor,

                        :bookings => actor.bookings.find(:all,
                           :conditions => [
                           'TO_DAYS(booked_at)=' <<
      @actors_tomorrow << {:actor=>actor,
                        :bookings => actor.bookings.find(:all,
                           :conditions => [
                           'TO_DAYS(booked_at)=' <<

This controller has just one action: index, which displays the bookings for today and tomorrow.

Creating the View for the Web Report

Next, let's create a view that actually displays this data, as shown in Listing 5-7.

Listing 5-7. The Single View for the Actor Scheduling Application (app/views/home/ index.rhtml)

  body { font-family: sans-serif }
  h2 { margin-left: 10pt;}
  p { margin-left: 10pt; }

<h1>Today's Schedule:</h1>

<% @actors_today.each do |actor_today| %>
    <h2><%= actor_today[:actor].name %></h2>
    <p><%if actor_today[:bookings].length > 0 %>
actor_today[:bookings].each do |b|
                            <%=b.booked_at.strftime('%I:%m%p') %>
                            <%=b.room.name %>,
                            <%=b.project.name %><br>
                            Nothing for today!

<% end %>

  <h1>Tomorrow's Schedule:</h1>

<% @actors_tomorrow.each do |actor_tomorrow| %>
    <h2><%= actor_tomorrow[:actor].name %></h2>
    <p><%if actor_tomorrow[:bookings].length > 0 %> 
actor_tomorrow [:bookings].each do |b|
                         <%=b.booked_at.strftime('%I:%m%p') %>,
                         <%=b.room.name %>,
                         <%=b.project.name %<br>
                         Nothing for tomorrow!

<% end %>

Save this file as app/views/home/index.rhtml.

Now you need just one more piece: a layout, which is used as a template. In other words, the view is displayed inside the layout. Listing 5-8 shows the layout.

Listing 5-8. Layout for the Actor Schedule View (app/views/layouts/application.rhtml)

<title>Actor Schedule Report</title> 
<%= yield %>

Save this as app/views/layouts/application.rhtml. This layout will be used automatically for all pages in the application by default. You can set up a layout for just one controller, and you can also manually override layouts for a given action.

At this point, the application is complete.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials