首页 > 编程语言 >Ruby实践—will_paginate实现分页

Ruby实践—will_paginate实现分页

时间:2023-06-09 11:07:19浏览次数:75  
标签:index Product paginate will products Ruby page


开发环境:

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实践—will_paginate实现分页_ruby

Ruby实践—will_paginate实现分页_windows_02

 

 

标签:index,Product,paginate,will,products,Ruby,page
From: https://blog.51cto.com/u_16129500/6445784

相关文章

  • ruby 访问 ceph
    #!/usr/bin/envruby#通过restfulAPIrequire"base64"require"openssl"require"net/http"PublicKey="publicKey"PrivateKey="privateKey"defgen_auth(http_method,bucket,filename,gmttime)sign=......
  • [ABC166E] This Message Will Self-Destruct in 5s
    ThisMessageWillSelf-Destructin5sの传送门Solution首先看到\(j-i=A_i+A_j\)转换一下,\(i+a_i=j-a_j\)。接下来,对于每一个\(i\)(\(1\lei\len\)),用一个map存\(i-a_i\)的数量。最后枚举\(i\)(\(1\lei\len\)),每次将\(ans\)加上\(i+a_i\)在map里的数......
  • git add 时报错 warning: in the working copy of 'package-lock.json', LF will...
    来源:https://blog.csdn.net/qq_43842093/article/details/128471953问题:执行gitadd.时报错: 原因:换行符的问题, Windows下换行符和Unix下的换行符不一样,git会自动转换。 解决办法: 执行如下命令:gitconfig--globalcore.autocrlffalse问题解决 ......
  • 在react里面刷新浏览器,不会触发componentWillUnmount事件
    今天遇见个小bug发现刷新浏览器,componentWillUnmount竟然不会触发。搜了一下,可能原因是浏览器刷新的时候,componentQillUnmout来不及触发,就被刷掉了。使用onbeforeunload事件可以完美解决这个问题。 ......
  • kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future versi
    问题现象为通过kubectl进入pods时提示在未来版本中将移除这种进入这种方式,需要使用新的命令格式进入[root@master~]#kubectlexecmyweb-c5xq6-it/bin/bashkubectlexec[POD][COMMAND]isDEPRECATEDandwillberemovedinafutureversion.Usekubectlexec[POD]--......
  • tflearn Training Step每次 We will run it for 10 epochs (t
    TrainingTFLearnprovidesamodelwrapper'DNN'thatcanautomaticallyperformsaneuralnetworkclassifiertasks,suchastraining,prediction,save/restore,etc...Wewillrunitfor10epochs(thenetworkwillseealldata10times)withabat......
  • Ruby实践—用户登录
    开发环境Ruby:Ruby1.9.1Rails:Rails2.3.5Mysql:Mysql5.0.9Driver:mysql-2.8.1-x86-mingw32.gemIDE:Rubymine2.0.1 一、创建View/login在View/login下创建login.html.erb、index.html.erb、loginFail.html.erblogin.html.erb代码如下:<h1>Welcometologin!</h1><%form_tagdo%......
  • RUBY-FLEX实践—利用swfobject在RUBY工程中加载SWF
    开发环境:Ruby:Ruby1.9.1Rails:Rails2.3.5IDE:RubyMine2.0.1FlexBuilder:FlexBuilder4 开发思路:1)在FlexBuilder中创建Flex工程2)在RubyMine中创建Rails工程3)将Flex工程bin-debug下编译的swfobject.js拷贝至Ruby工程指定位置4)引用Flex工程编译后的html中的内容实现在Rails页面中嵌......
  • Ruby教程_编程入门自学教程_菜鸟教程-免费教程分享
    教程简介Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(YukihiroMatsumoto)开发,遵守GPL协议和RubyLicense。它的灵感与特性来自于Perl、Smalltalk、Eiffel、Ada以及Lisp语言。由Ruby语言本身还发展出了JRuby(Java平台)、IronRuby(.NET......
  • ruby小提示
    目录删除要删除目录,请使用Dir.rmdir。但是,与命令rmdir一样,此方法只能删除空目录。Dir.rmdir("dir")因此,如果不递归删除内容,则无法删除包含内容的目录。fileutils库中的FileUtils.rm_r会为您完成这项工作。reqiure'fileutils'FileUtils.rm_r("dir")即使使......