Ruby實踐—will_paginate實現分頁

來源:互聯網
上載者:User

開發環境:

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<br /> # @products = Product.all<br /> @products = Product.paginate :page => params[:page]||1, :per_page => 2<br /> respond_to do |format|<br /> format.html # index.html.erb<br /> format.xml { render :xml => @products }<br /> end<br /> 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' %>

 

運行結果:

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.