我用will_paginate使用newes Rails 3版本。Rails 3:Will_paginate的.paginate不起作用
@videos = user.youtube_videos.sort.paginate :page => page
我還添加了@@per_page
屬性我youtube_video模型。
但它只是不會分頁。我總是列出集合中的所有項目。
我做錯了什麼?
你的,喬恩。
我用will_paginate使用newes Rails 3版本。Rails 3:Will_paginate的.paginate不起作用
@videos = user.youtube_videos.sort.paginate :page => page
我還添加了@@per_page
屬性我youtube_video模型。
但它只是不會分頁。我總是列出集合中的所有項目。
我做錯了什麼?
你的,喬恩。
這裏是我的解決辦法,我自己的答案,對所有其他的具有will_paginate麻煩和閱讀這個問題:
創建一個ApplicationController的方法是這樣的:
def paginate_collection(collection, page, per_page)
page_results = WillPaginate::Collection.create(page, per_page, collection.length) do |pager|
pager.replace(collection)
end
collection = collection[(page - 1) * per_page, per_page]
yield collection, page_results
end
然後在你的控制器,在哪兒買
page = setup_page(params[:page]) # see below
@messages = Message.inbox(account)
paginate_collection(@messages, page, Message.per_page) do |collection, page_results|
@messages = collection
@page_results = page_results
end
並且在視圖:應該分頁集合
<% @messages.each do |message| %>
<%# iterate and show message titles or whatever %>
<% end %>
<%= will_paginate @page_results %>
要獲得規定的page
變量,檢查:
def setup_page(page)
if !page.nil?
page.to_i
else
1
end
end
所以page = setup_page(params[:page])
的伎倆,與簡單的方法。
This WORKS!
你爲什麼在這裏打電話sort
?這似乎是不必要的,可能會導致它找到所有視頻並調用分頁,而不是注意任何在您的Video
模型中定義的變量。相反,通過使用示波器或使用order
方法將排序邏輯移動到Video
模型中。
我這樣做,但它並沒有幫助反正...而不是排序,幷包括Comparable,我只是提出了一個名爲「all_for_user」的新模型實例方法,如下所示:YoutubeVideo.all_for_user(user).paginate:page => page。 ..但沒有成功,同一問題 –
@Joern:你爲什麼要這麼做? YoutubeVideo的表格上沒有「user_id」列,因此你可以做'user.youtube_videos'或類似的東西? –
這是我第一次做的,也沒有與paginate一起工作。這樣我也需要對它進行排序......我更喜歡創建自己的方法,在這裏我可以明確地定義應該發生什麼。 –
這是沒有足夠的信息來幫助你。 ':page => page'中的'page'從哪裏來?我也不認爲在這裏使用'sort'是個好主意。如果你想排序數據庫記錄,最好使用':order => ...'。 – Mischa
page = setup_page(params [:page])| => |如果!arg1.nil?然後返回arg1.to_i else返回1 end |其中arg1是params [:page] –
使用will_paginate gem的'〜> 3.0.pre4'版本。然後像這樣分配per_page:'YoutubeVideo.per_page = 10'。它應該工作! – mislav