RUBYʵ¼ù—Ruby ReportÖ®Ruport¼òµ¥Ó¦ÓÃ
Ruport¹Ù·½ÍøÕ¾£ºhttp://www.rubyreports.org/
±¾ÀýÖн«½éÉÜRuby±¨±íµÄ¼òµ¥¿ª·¢
¿ª·¢»·¾³
OS:WindowsXP
Ruby:Ruby1.8.7
Rails:Rails2.3.5
Mysql:Mysql5.0.9
IDE:Rubymine2.0.1
Ò»¡¢°²×°gem
°²×°ÃüÁî
gem install ruport
gem install ruport-util
gem install acts_as_reportable
¶þ¡¢´´½¨Êý¾Ý¿â
database: dbdevelopment
username: crystal
password: crystal
host: localhost
Èý¡¢´´½¨Rails¹¤³ÌRailsRuport
1£©ÅäÖÃdatabase.yml£¬ÄÚÈÝÈçÏ£º
development:
adapter: mysql
encoding: utf8
reconnect: false
database: dbdevelopment
pool: 5
username: crystal
password: crystal
host: localhost
2£©Í¨¹ýscaffoldÓ³ÉäProducts±í
²ÎÊýΪ Product title:string description:string price:integer
3£©ÐÞ¸Äroutes.rb
ÐÞ¸Ä
map.resources :products
Ϊ
map.resources :products,:collection=>{:save_as_report=>:get}
±íʾµ±Óöµ½save_as_reportʱ£¬ÓÃget·½Ê½£¬·ñÔòĬÈÏ·½Ê½½«Ìø×ªµ½show.htmlÖ´Ðвéѯ
ÔÚ×îºóÌí¼Ó
require "rubygems"
require "ruport"
ËÄ¡¢ÐÞ¸ÄProduct.rb
ΪModelÌí¼Óacts_as_reportable·½·¨
Ð޸ĺó´úÂëÈçÏ£º
class Product < ActiveRecord::Base
acts_as_reportable
set_primary_key "product_id"
end
Îå¡¢ÐÞ¸Äproducts_controller.rb
1£©ÐÞ¸Äindex·½·¨ÎªÈçÏ£ºÌí¼ÓReportµÄÊä³öÓ¦ÓÃ
def index
@products = Product.all
@table = Product.report_table(:all,:only=>['title','description'])
@grouping = @table.to_group('title')
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
end
2£©Ìí¼Ósave_as_report·½·¨£¬ÊµÏÖReportµÄ±£´æÓ¦ÓÃ
def save_as_report
puts 'Save Pdf!'
send_data Product.report_table(:all,:only=>['title','description']).to_pdf, :type => "application/pdf",
:filename => "books.pdf"
end
Áù¡¢ ÐÞ¸Äapp/view/products/index.html.erb
ÔÚ½çÃæÉÏÏÔʾReport»æÖƵÄtable£¬ÔÚ×îºóÌí¼ÓÈçÏ´úÂ룺
<h2>Report Table</h2>
<%= @grouping.to_html %&
Ïà¹ØÎĵµ£º
ÕâÊÇÒ»¸ö¿ªÔ´£¬Ãâ·Ñ£¬ÁéÇÉ£¬¼òÒ×µÄweb×Ô¶¯»¯²âÊÔ×éºÏ¿ò¼Ü£»Í¨³£Çé¿ö£¬¿ªÔ´µÄ¶«Î÷×ÜÊǸøÈËÒÔÃÔÈË£¬¿ª·ÅʽµÄ´¥¸Ð£¬µ«Í¬Ê±£¬Ò²ÈÃÈËÄÑÒÔ¿ìËÙ½Ó½ü£¨ÏñÊǶ«·½ÄÐÈË¿´´ýÎ÷·½ÃÀÅ®£©£»ÒªÁ˽âÒ»¸ö¿ªÔ´¶øÇÒ×éºÏƯÁÁµÄ¿ò¼Ü£¬×ÜÊÇÒª·ÑЩÖÜÕÂ......×ܶøÑÔÖ®£¬Ëü²»»áÏñÊշѵĶ«Î÷£¬ÄãºÜÌÖÑáËü£¬µ«²»¿É·ñÈÏ£¬ÄãµÄ¸¶·ÑÆäʵ»»È¡Á˱ðÈ˵ÄËöËéÀͶ ......
ʹÓà will_paginate ½øÐзÖÒ³ºÍ¼òµ¥²éѯ
ÔÚÃüÁîÐÐÏÂʹÓà gem install will_paginate ÃüÁ³öÏÖÏÂÃæ½á¹û°²×°³É¹¦
´ò¿ª books_controller.rb (Äã×Ô¼ºµÄ¿ØÖÆÆ÷)
×¢Ê͵ô²éÕÒÈ«²¿µÄ·½·¨£¬Ê¹ÓÃÏÂÃæµÄ·½·¨£¬ÒѾ¼¯³É¸ù¾Ýtitle½øÐвéѯ
Ruby´úÂë
#@books = Book.all
@books = Book.pagina ......
ǰÑÔ
ÔÚ¡¶RoutingµÄÔØÈë¡·ÖУ¬ÎÒ´óÖ½éÉÜÁËÒ»ÏÂRailsÖÐ×î¼òµ¥µÄrouteÊÇÈçºÎ¼ÓÔØµÄ¡£ÕâÆªÎÄÕ£¬ÎÒ½«À´½²Ò»½²RailsϵͳÖиüΪ¸´ÔÓµÄnamed routeºÍÓëRESTfulÏà¹ØµÄresourceÊÇÈçºÎ±»¼ÓÔØµÄ¡£ÎªÁ˲»Öظ´Ì«¶àµÄ±ÊÄ«£¬ÕâÆªÎÄÕ½«ÔÚǰÎĵĻù´¡ÉϽøÐУ¬Èç¹û·¢ÏÖµ¥¶À¿´´ËÎÄʱ£¬ÓÐÉÙÐíÔÆÀïÎíÀ½¨ÒéÏÈ¿´Ò»¿´ÎÒµÄǰƪÎÄÕ£ºR ......
¿ª·¢»·¾³
Ruby:Ruby1.9.1
Rails:Rails2.3.5
Mysql:Mysql5.0.9
Driver:mysql-2.8.1-x86-mingw32.gem
IDE:Rubymine2.0.1
Ò»¡¢´´½¨View/login
ÔÚView/loginÏ´´½¨login.html.erb¡¢index.html.erb¡¢loginFail.html.erb
login.html.erb´úÂëÈçÏ£º
<h1>Welcome to login!</h1>
<% form_tag do %>
& ......
http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Understanding Ruby Blocks, Procs and Lambdas
Blocks, Procs and lambdas (referred to as closures
in Computer Science) are one of the most powerful aspects of Ruby, and
also one of the most misunderstood. This ......