Forum: Ruby on Rails Question on creating a report

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-03-19 07:29
(Received via mailing list)
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
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-03-19 07:29
(Received via mailing list)
Hi !

2006/3/18, John Tsombakos <johnt519@gmail.com>:
>
>
> 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:
<h1>Report for <%= Date.today.to_formatted_s %></h1>
...

Hope that helps !
This topic is locked and can not be replied to.