Question on creating a report


I’ve got a little Rails app, and I wanted to generate a report from
some data. I did it one way, but it didn’t seem quite “Rails”.

I created two methods in a controller, one called “report” that was

def report


that would just then display the report.rhtml page. This page
contained a form tag with a text box and a submit button. This form
then submitted to a “do_report” method in the controller:

def do_report

teams = … etc

This then displayed a do_report.rhtml which formatted the output.

I thought I would try to streamline it a bit, and moved the code in
do_report into the report method, and had the and had the form submit
directly to the “report” method and used:

if …


to only generate the report data if it was posted.

I also removed the do_report.rhtml file, and moved it’s code into the
report.rhtml file, but surrounded the reporting section with

<% if … %>

<% end %>

This seems kludgy to me and was wondering how others have handled
this. The separate “report” and “do_report” seem even more cumbersome.

Does anyone have any hints/suggestions/sample code?



Hi !

2006/3/18, John T. [email protected]:

I also removed the do_report.rhtml file, and moved it’s code into the



Lately, I have been implementing my new/edit actions this way:

class SomethingController < ApplicationController
def new
@object =
return unless

if @object.update_attributes(params[:object]) then


You can use the same trick in your report method.

Second, use a partial for your view code, and include the partial only
if the report’s data is available. Something that might help you:

def report
return unless

build report

Flag the report as being available

@report_available = true

<%= start_form_tag %>

<%= submit_tag ‘Rebuild’ %>
<%= end_form_tag %>
<%= render(:partial => ‘report’) if @report_available %>


Report for <%= %>


Hope that helps !

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs