# Question on creating a report

**URL:** <https://www.ruby-forum.com/t/question-on-creating-a-report/55293>\
**Category:** Rails\
**Created:** [March 19, 2006, 7:29am UTC](https://www.ruby-forum.com/t/question-on-creating-a-report/55293 "2006-03-19T07:29:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [March 19, 2006, 7:29am UTC](https://www.ruby-forum.com/t/question-on-creating-a-report/55293/1 "2006-03-19T07:29:26Z")

</div>

Hi,

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  
empty:

def report

end

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

end  
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 request.post? …  
…  
end

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?

Thanks!

jt

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [March 19, 2006, 7:29am UTC](https://www.ruby-forum.com/t/question-on-creating-a-report/55293/2 "2006-03-19T07:29:45Z")

</div>

Hi !

2006/3/18, John T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I also removed the do\_report.rhtml file, and moved it’s code into the
> 
> Thanks!
> 
> jt

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

class SomethingController \< ApplicationController  
def new  
@object = Object.new  
return unless request.post?

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

```

end  
end

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 request.post?

# build report

# Flag the report as being available

@report\_available = true  
end

report.rhtml:  
\<%= start\_form\_tag %\>  
…  
\<%= submit\_tag ‘Rebuild’ %\>  
\<%= end\_form\_tag %\>  
\<%= render(:partial =\> ‘report’) if @report\_available %\>

\_report.rhtml:

# Report for \<%= Date.today.to\_formatted\_s %\>
...

Hope that helps !
