简体   繁体   English

分页问题(will_paginate)

[英]Trouble with pagination (will_paginate)

I'm trying to paginate with will_paginate in my rails app. 我正在我的Rails应用程序中尝试使用will_paginate进行分页。

in my controller I have 在我的控制器中

def index
    params[:per_page] ||= 25
    params[:page] ||= 1
    @links = Link.order('created_at DESC').page(params[:page]).per_page(params[:per_page])
end

in my view I have 我认为我有

 <ul>
  <% @links.each do |entry| %>
  <li><%= link_to entry.title, entry.url %></li>
  <% end %> 
<ul>
<%= will_paginate @links %>

I'm getting the error 我遇到了错误

comparison of Fixnum with String failed

Extracted source (around line #8):

5: </h2>
6: 
7: <ul>
8:   <% @links.each do |entry| %>
9:   <li><%= link_to entry.title, entry.url %></li>
10:   <% end %> 
11: <ul>

I have no idea why. 我不知道为什么。 I have tried restarting the server. 我尝试重新启动服务器。 Am I missing something obvious? 我是否缺少明显的东西?

Thanks in advance. 提前致谢。

I'm guessing that you have a simple "everything in params is a String" problem, that could explain why someone is trying to compare a Fixnum with a String. 我猜想您有一个简单的“ params中的所有内容都是字符串”的问题,这可以解释为什么有人试图将Fixnum与字符串进行比较。 The page and per-page values should be Fixnums but, if they come from params , they will be strings. page和per-page值应为Fixnums,但如果它们来自params ,则它们将为字符串。 The easiest thing to do is to add a couple to_i calls as indicated: 最简单的方法是按照指示添加几个to_i调用:

def index
    params[:per_page] ||= 25
    params[:page] ||= 1
    @links = Link.order('created_at DESC').page(params[:page].to_i).per_page(params[:per_page].to_i)
    #---------------------------------------------------------^^^^
    #------------------------------------------------------------------------------------------^^^^
end

The query that paginate produces won't be evaluated until you try to get something from it, that's why you don't get the error until you @links.each in your template. 在尝试从分页中产生的查询之前,不会对其进行评估,这就是为什么直到在模板中@links.each之前都不会收到错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM