2013-10-08 20 views
0

我正在使用sinatra和小鬍子來構建應用程序。我想在頁面上顯示最多30個項目。我正在考慮will_paginate寶石,但它似乎很複雜。有沒有更簡單的方法來做到這一點?這是我links.rb觀點:如何輕鬆顯示頁面上的項目數量有限,並添加到更多頁面的鏈接

module S 
    class App 
    module Views 
     class AdminLinks < Layout 

     def links_all 
      Links.all.map do |s| 
      { 
       :name => s.name, 
       :comments_num => s.comments.count, 
       :user_name => s.user ? s.user.name : "" , 
       :created_at => s.created_at.to_formatted_s(:db) , 
       :user_id => s.user ? s.user.id : "" , 
       :streme_id => s.id, 
      } 
      end 
     end 

     end 
    end 
    end 
end 

我想知道如何使用:Links.limit(5).offset(5)

回答

1

對於裸骨分頁可以使用

# in controller 
@page = params[:page] ? 1 : params[:page].to_i 


# in model 
def links_all(page) 
     per_page = 30 
     Links.limit(per_page).offset((page - 1) * per_page).all.map do |s| 
     { 
      :name => s.name, 
      :comments_num => s.comments.count, 
      :user_name => s.user ? s.user.name : "" , 
      :created_at => s.created_at.to_formatted_s(:db) , 
      :user_id => s.user ? s.user.id : "" , 
      :streme_id => s.id, 
     } 
     end 
end 

# in view (replace links_path with your path helper 
<%= link_to "Previous Page", links_path(:page => @page - 1) if @page > 1 %> 
<%= link_to "Next Page", links_path(:page => @page + 1) %> 
+0

謝謝,但我應該在哪裏放這個代碼? – lipenco

+0

更新了答案 – tihom

相關問題