Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialalborz
Full Stack JavaScript Techdegree Graduate 30,885 PointsGetting an error when attempting to implement will_paginate gem
I've installed the will_paginate gem on my app, which is supposed to display some links, but when I run the server in development and access the home page I get the following error:
NoMethodError in Pages#index
undefined method `total_pages' for # Link::ActiveRecord_Relation:0x007fc8a3821ec8
The error highlights the 'will_paginate' line of my index view:
<h1>Home page.</h1>
<ul>
<% @links.each do |link| %>
<li><%= link_to link.title, link.url %></li>
<% end %>
</ul>
<%= link_to "Submit a new link", new_link_path %>
<% # Render pagination in the view %>
<%= will_paginate @links %>
Here is my Links controller:
class LinksController < ApplicationController
def index
@links = Link.all
# Use an explicit "per page" limit:
@links = Link.all.paginate(:page => params[:page], :per_page => 30)
end
def new
@link = Link.new
end
def create
@link = Link.new(link_params)
if @link.save
redirect_to @link
else
render 'new'
end
end
def show
@link = Link.find(params[:id])
end
private
def link_params
params.require(:link).permit(:title, :url)
end
end
Thanks!
1 Answer
Gavin Ralston
28,770 PointsGive this a try in the index function, as it looks like the syntax you want to use:
@links = Links.paginate(:page => params[:page], :per_page => 30)
alborz
Full Stack JavaScript Techdegree Graduate 30,885 Pointsalborz
Full Stack JavaScript Techdegree Graduate 30,885 PointsUnfortunately I wasn't able to solve the issue
Ross Litzenberger
11,706 PointsRoss Litzenberger
11,706 Points@links = Links.paginate(page: params[:page], per_page: 30)
Gavin Ralston
28,770 PointsGavin Ralston
28,770 PointsThat's the same thing as I suggested, just not using hash rockets, right?
There's a lot of discussion about that exact error with the gem on stack overflow and other places. Something's got to work. :/
alborz
Full Stack JavaScript Techdegree Graduate 30,885 Pointsalborz
Full Stack JavaScript Techdegree Graduate 30,885 PointsThanks all, I'll keep digging.
Gavin Ralston
28,770 PointsGavin Ralston
28,770 PointsGoogle that exact error, minus the memory address, and you should find a couple of lengthy discussions. Something is bound to work, and will likely increase your comfort/understanding of rails in the process.
Ross Litzenberger
11,706 PointsRoss Litzenberger
11,706 Points@GavinRalston Yes its the same, I just wanted to show the other way.
alborz
Full Stack JavaScript Techdegree Graduate 30,885 Pointsalborz
Full Stack JavaScript Techdegree Graduate 30,885 PointsHmm I seem to have left it in there when adding the line for pagination. Got rid of the the first one yet still didn't work. Anyway, decided to scratch will_paginate and decided to start again using bootstrap with pagination.