Generate Report

Hello guys, I’m 100% newbie in RoR. I have a question, how can I generate report in RoR like crystal report?

that’s a nice broad question - so I’ll give you a broad answer (the very MVP with lots of room for optimization)

:slight_smile:

(and I’ve probably left one or two BIG mistakes - and my apologies for that)

cheers Walther

—— here goes:

if you by report mean a list of, say accounts, each listing eg. transactions like this

  sales inland     q1 - transactions       1/1/15 burning red 1,500.00       1/3/15 smoothers 252.75     q2 - transactions       4/21/15 broke back 975.00

I’d make myself a report action in the accounts controller (like /accounts/report) or use the :index action and supply an argument when you call it (like /accounts?report=true) or construct a special ‘decorator’-kind of controller (like ReportsController) and call it like /reports?accounts=true

Your controller (let’s stick with the ReportsController for now)

  def accounts     @collection = Account.all.includes :transactions   end

Add a route in routes.rb

  ressources :reports do     collection do       get :accounts     end   end

In your views (ie app/views/reports) I would build the report in account.html.haml

%table   %thead     %tr       %th t(‘report.account.quarter')       %th= t(‘report.account.transaction’)   %tbody     = @collection.each do |account|       = render partial: ‘account’, locals: { account: account }

Each account gets built in app/views/accounts/_accoount.html.haml

%tr   %td= account.title   %td

- quarter = 1 - account.transactions.sort(:transaction_date) do |transaction|   - q = find_quarter(transaction)   - if q > quarter     - quarter = q     %tr       %td.quarter= “q%s - transactions” % say_quarter transaction       %td         %table.transactions   %tr     %td.transaction-date= transaction.transaction_date     %td.transaction-text= transaction.description     - if transaction.amount < 0       %td       %td.transaction-amount= transaction.amount     - else       %td{ colspan: ‘2’}.transaction-amount= transaction.amount

Then you could order it with

  = link_to t(‘report.on-accounts’), “#!”, data: { url: "<on of the url’s listed above>”}, class: “report”

And in the bottom of your index.html.haml (or what ever your page with the above link is) you’d put

:coffeescript

  $ ->     $(document.body).on ‘click’, ‘.report’, (e) ->       elem = $(e.currentTarget)       window.open elm.data(‘url’)

Hey Rodney,

I’ve been using RoR for a couple months (still quite green). You should look for a gem to do what you’re looking for, start looking at https://rubygems.org/.

Onward.

A common solution is generating the report in HTML and converting it to PDF with these gems:

https://github.com/mileszs/wicked_pdf

https://github.com/zakird/wkhtmltopdf_binary_gem

+1 on this! I’ve used Wicked and it worked out really well. I’ve also used prawn to generate PDF reports as well but I sort of lean towards WickedPDF.