JacobJuul
JacobJuul

Reputation: 152

Trouble with pagination (will_paginate)

I'm trying to paginate with will_paginate in my rails app.

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.

Upvotes: 2

Views: 826

Answers (1)

mu is too short
mu is too short

Reputation: 434945

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. The page and per-page values should be Fixnums but, if they come from params, they will be strings. The easiest thing to do is to add a couple to_i calls as indicated:

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.

Upvotes: 7

Related Questions