易截截图软件、单文件、免安装、纯绿色、仅160KB

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 %&


相关文档:

ruby调用平台指令删除指定文件夹

正常情况下,ruby如果发现文件夹中有system,readonly,hide类型的文件,则是不能直接删除
他们的。如何解决呢?
以其人之道还治其人之身!
def dryf(fpath) #destroy file
x = Iconv.iconv('utf-8','gbk',fpath).to_s
%x{attrib -R -S -H \"#{Iconv.iconv('gbk','utf-8',x).to_s}\"}
f = File.new(f ......

Ruby实践—连接Mysql

安装环境:
OS:Windows XP
Ruby: Ruby1.9.1
Mysql: Mysql5.1.46 (username/password: root/root  port:3306)
Ruby-Mysql Driver: mysql-2.8.1-x86-mswin32.gem
(注:用2.7.3版本的驱动在测试时会出现 require"mysql",找不到指定模块 错误)
IDE:RubyMine2.0.1
安装Ruby,RubyMine,Mysql的事项在这里就不多 ......

Ruby实践—will_paginate实现分页

开发环境:
OS:Windows XP
Ruby:Ruby1.9.1
Rails:Rails2.3.5
will_paginate:will_paginate2.3.11
(在命令行中运行 gem install mislav-will_paginate --source http://gems.github.com )
IDE:Rubymine2.0.1
DB:mysql5.0.9
本例在上一个例子(Ruby实践—简单数据库操作)的基础上实现分页,利用的是will_p ......

ruby随机在控制台显示字符串

require 'curses'
module Curses
def self.program
main_scr=init_screen
noecho
cbreak
curs_set(0)
main_scr.keypad=true
yield main_scr
end
end
Curses.program do |scr|
max_x=scr.maxx
max_y=scr.maxy
100.times do
scr.setpos(rand(max_y),rand(max_x))
......

Ruby下解决oci8安装错误的问题

Prepare
1. Download 'OCI 1.x.x.rb' (it's VERY important - execution MUST be *.rb)
2. Download 'oci8lib.so'
Install
1. Copy 'OCI8.rb' to .../ruby/lib/ruby/site_ruby/1.8/DBD/OCI8
2. Copy 'oci8.rb' to .../ruby/lib/ruby/site_ruby/1.8
3. Copy 'oci8lib.so' to .../ruby/lib/ruby/site_ruby/1.8/i386-msv ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号