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

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' %>
运行结果:
 


相关文档:

Ruby声音


转 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 ......

Ruby和Python的语法比较


Ruby和Python的语法比较
 
 
 
其实Ruby和Python非常接近,比大多数别的语言要接近的多,所以喜欢用啥就用啥(大实话,虽然也是废话)。语法上的差别虽然有那么一点,大部分是syntax sugar,我斗胆稍微列几个(python我也忘得差不多了,不对的大家尽管来鞭尸吧),但是主要差异还是设计思想上的:灵活 ......

Ruby on Rails中select使用方法

在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆。常见的有三个..
select, select_tag, collection_select(其余的什么select_date那些不谈)
我们先来看看一个基本的下拉式选项骨架
</p>
<select
name="ROR">
<option
value="1">ROR1</option><br
/>
<optio ......

ruby类变量在development模式失效

分页中用到类变量,主要是用来标记“页码输入框”的id 如果一个页面有几个分页,“页码输入框”的id要是不同的才能分清是哪个要分页。使用类变量就是为了达到这个目的,让所有的对象实例共用一个变量,不必每次重新初始化变量。 类变量使用代码示例 1 require 'ruby-debug'
2 debugger
3 cla ......

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的事项在这里就不多 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号