[英]Adding pagination to index.html.erb in Ruby on Rails - Easy Question
我是 Rails 的新手,所以放輕松。 我已經建立了我的第一個博客,並希望在 <% post.each do |post| 中設置它。 %> 呈現帖子,我希望它能夠工作,使其只顯示 10 個帖子,然后有一個鏈接來查看接下來的 10 個帖子。
我希望這是一個簡單的問題。 如果您希望我發布任何代碼,請告訴我。
您應該使用 gem will_paginate 。
安裝和使用真的很簡單: https : //github.com/mislav/will_paginate/wiki/installation
will_paginate絕對是要走的路,我只是想我會添加一個指向railscasts will_paginate 視頻的鏈接,向您展示如何使用它,因為有時這比閱讀文檔更容易學習基礎知識。 此外,您還將了解分頁作為 Rails 的一部分時過去如何完成的簡要歷史(它更慢且更麻煩)。 舊的經典分頁也已移出到它自己的插件中,但僅當您將 Rails 升級到他們刪除它的版本時已經在使用它時才建議使用它。
Railscasts 有大量其他很棒的視頻,您可能會覺得它們很有幫助。 例如,一旦你變得更高級,你可能想嘗試ajax 分頁
查看will_paginate寶石。
在 ActiveRecord 模型上進行分頁非常容易:
@posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
在視圖中,可以使用單個視圖助手呈現頁面鏈接:
<%= will_paginate @posts %>
我在使用“will_paginate”時遇到問題,安裝 GEM 然后卸載……一個真正的問題! 所以我決定在 RoR 上編寫分頁程序,這並不難,所以我想分享我所做的:
控制器:
def index
#how many register per page i want to show
@b = 30
#params via GET from URL
a = params[:page].to_i
#first register per page
a1 = params[:page].to_i * @b
#the query can be easy...
@callcenters = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :order => "created_at DESC",:limit => "#{a1},#{@b}", :select => "radcheck.username as clave,caller_id, created_at, value")
#I need to know how many pages somehow
@registrostotales = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :select => "radcheck.username as clave,caller_id, created_at, value").count
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @callcenters }
end
end
看法:
...
Total de registros: <%= numero = @registrostotales %><br/><br/>
<!-- total pages -->
<% pages = @registrostotales / @b %>
<!-- the last page -->
<% if pages % @b > 0 %><% pages = pages + 1 %><% end %>
Paginas:
<% (0..(pages-1)).each do |i| %>
<!-- href or link_to, http://xxxxxxxx/yyyy?page=i -->
<%= link_to i+1, :action => "index", :controller => "callcenters", :page => i %>
<% end %>
<!-- the view.. -->
<table>
<tr>
<th>#</th>
<th>Teléfono (ID)</th>
<th>Zona</th>
</tr>
<% @callcenters.each do |callcenter| %>
<tr>
<td><%= numero - params[:page].to_i * 30 %><% numero = numero - 1 %></td>
<td><%= callcenter.caller_id %></td>
<td><%= Node.first(:conditions => {:calling_id => callcenter.caller_id}).description %></td>
</tr>
<% end %>
</table>
我希望這對某人有所幫助!
最好的問候,伊萬卡拉斯科克
我們可以通過使用 'will_paginate-bootstrap' gem 輕松地做到這一點。
要繼續,首先將一行添加到 gem 文件中,
gem 'will_paginate-bootstrap'
。
現在運行捆綁安裝。
在控制器中,您將使用 like @post = Post.all
從模型中獲取內容。 而是使用這個
@post = Post.paginate(:page=>params[:page],per_page:5)
上面一行中的 per_page 字段是指定一個頁面需要多少條記錄。
<%= will_paginate @post,renderer: BootstrapPagination::Rails %>
注意:我認為 Post 是模態的,post 是控制器中聲明的變量。 只需使用您的變量即可。
向 Ruby on Rails 應用程序添加分頁
要將分頁添加到您的 Ruby on Rails 應用程序,您必須修改以下文件:
寶石檔案:
gem 'will_paginate', '~> 3.1.0'
gem 'will_paginate-bootstrap'
區域控制器 --> 索引
@areas = Area.pagination_request(params[:page])
index.html.erb
<%= will_paginate @areas, renderer: BootstrapPagination::Rails %>
模型文件:
def self.pagination_request(page)
paginate :per_page => 10, :page => page
end
或者,小,快速且積極維護: Pagy
您可以使用 kaminari gem。
非常易於使用和高度定制友好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.