Ruby practice-ruport simple application of Ruby report

Source: Internet
Author: User

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:

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.