Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

alborz
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
alborz
Full Stack JavaScript Techdegree Graduate 30,884 Points

Getting 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
Gavin Ralston
28,770 Points

Give 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)
Ross Litzenberger
Ross Litzenberger
11,706 Points
@links = Links.paginate(page: params[:page], per_page: 30)
Gavin Ralston
Gavin Ralston
28,770 Points

That'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. :/

Gavin Ralston
Gavin Ralston
28,770 Points

Google 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
Ross Litzenberger
11,706 Points

@GavinRalston Yes its the same, I just wanted to show the other way.

alborz
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
alborz
Full Stack JavaScript Techdegree Graduate 30,884 Points

Hmm 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.