Ruport Official Website: http://www.rubyreports.org/
This example introduces the simple development of Ruby reports.
Development Environment
OS: Windows XP
RUBY: ruby1.8.7
Rails: rails2.3.5
MySQL: mysql5.0.9
IDE: rubymine2.0.1
1. Install gem
Installation command
Gem install ruport
Gem install ruport-util
Gem install acts_as_reportable
2. Create a database
Database: dbdevelopment
Username: Crystal
Password: Crystal
HOST: localhost
3. Create a rails project railsruport
1) Configure database. yml with the following content:
Development:
Adapter: MySQL
Encoding: utf8
Reconnect: false
Database: dbdevelopment
Pool: 5
Username: Crystal
Password: Crystal
HOST: localhost
2) map the Products table through Scaffold
The parameter is product title: String Description: String price: integer.
3) Modify routes. Rb
Modify
Map. Resources: Products
Is
Map. Resources: Products,: Collection =>{: save_as_report =>: get}
When save_as_reportis returned, the getmethod is used. If the negative method is used, the list_html method is used to execute the query.
Add at last
Require "rubygems"
Require "ruport"
4. modify product. Rb
Add the acts_as_reportable Method to the model.
The modified code is as follows:
Class product <activerecord: Base <br/> acts_as_reportable <br/> set_primary_key "product_id" <br/> end
5. Modify products_controller.rb
1) Modify index as follows: add report output application
Def index <br/> @ products = product. all <br/> @ table = product. report_table (: All,: Only => ['title', 'description']) <br/> @ grouping = @ table. to_group ('title') </P> <p> respond_to do | format | <br/> format.html # index.html. ERB <br/> format. XML {render: XML =>@ products} <br/> end </P> <p>
2) Add the save_as_report method to save the report.
Def save_as_report <br/> puts 'Save PDF! '<Br/> send_data product. report_table (: All,: Only => ['title', 'description']). to_pdf,: TYPE => "application/pdf", <br/>: filename => "books.pdf" </P> <p> end
6. Modify APP/View/products/index.html. ERB
Display the table drawn by the report on the page, and add the following code at the end:
<H2> report table </H2> <br/> <% = @ grouping. to_html %> <br/> <% = link_to 'Save As report',: controller => "Products ",: action => "save_as_report" %>
Demo effect: