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_paginate插件
一、安装will_paginate
(在命令行中运行 gem install mislav-will_paginate --source http://gems.github.com )
二、修改enviroment.rb
引用"will_paginate",在
Rails::Initializer.run do |config|
end
之后添加 require 'will_paginate' ,否则运行时报错“method not found 'paginate' ”
三、修改product_controller.rb
修改 index 方法为如下:
def index
# @products = Product.all
@products = Product.paginate :page => params[:page]||1, :per_page => 2
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
end
#注:1是用户以http://localhost:3000/products 显示的第1页的数据;2是每页显示的记录数
@product_pages = Product.paginate :page => params[:page]||1, :per_page => 2
四、修改index.html.erb
添加如下引用
<%= will_paginate @products, :prev_label => 'pre', :next_label => 'next' %>
运行结果:
相关文档:
转 Adding Sound to Your Ruby Apps
Have you ever thought about including sounds in your Ruby application? Used sparingly, sound may enhance your applications by adding audio cues or a custom touch. You could, for example, play a beep or chime that announces the completion of a lengthy process. Per ......
self上下文
Ruby的self有和Java的this相似之处,但又大不相同。Java的方法都是在实例方法中引用,所以this一般都是指向当前对象的。而Ruby的代码逐行执行,所以在不同的上下文(context)self就有了不同的含义,先来看看常见的context self都代表哪些
1
2
3
4
5
6
7
8
9
10
11
12
13
......
转自 http://www.advidea.cn/biancheng/200943135232.html
Ruby watir 测试框架
大多数人都会安装 ruby,
也通过Ruby 安装 gem,
也安装了ruby IDE开发工具:netbeans,
但就是不能跑watir环境,狂晕加吐中。。。
错误如下:
in `require': no such file to load -- watir (LoadError)
反正就是找不到watir,这里 ......
1.1.Rails
1.1 创建一个Rails应用程序
$ rails app_name
可选项:
-d, database=xxx 指定安装一个数据库(mysql oracle postgresql sqlite2 sqlite3 ), 默认情况下是数据库
-r, ruby-path= 指定Ruby的安装路径,如果没有指定,scripts使用env去找Ruby
-f, freeze (冻结)freezes Rails在vendor/rails目录
1.2 API Docume ......
class Point
@x = 1
@y = 2
def initialize(x,y)
@x,@y = x,y
end
end
代码中的@x,@y为实例变量,实例变量只对self的环境起作用,因此initialize外面的@x=1,@y=2只对类本身起作用,而方法内部,的@x,@y是对对象的实例起作用的。
class Point
include Enumerable
def initialize(x ......