Question on creating a report


#1

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


#2

Hi !

2006/3/18, John T. 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 !