'will_paginate undefined method' in views but not controller - Sinatra 2.0, ActiveRecord 5.0

162 Views Asked by At

I added the will_paginate gem as outlined in the gem installation for Sinatra. I never get a 'no method error' inside the controller, but in the routed view I keep getting a 'no method for will_paginate #'. Answers related to Rails (link1, link2) are not fixing this. This mongoid related question came close.

Here's an outline of the code used:

In controller app:

require 'will_paginate'
require 'will_paginate/active_record'

class SinatraApp < Sinatra::Base

  get '/post' do
    @post = Post.all.paginate(:page => params[:page], :per_page => 30)
    erb :index
  end

end

In erb index view:

<%= will_paginate @posts %>

In Gemfile:

gem 'will_paginate', '~> 3.1.0'

Like I said, it only breaks in the view and not in the controller which leads me to believe it is a Sinatra specific case.

1

There are 1 best solutions below

1
On

My solution was this:

In controller app

class Post < Sinatra::Base

  configure do
    register WillPaginate::Sinatra
  end

end

This is definitely a Sinatra::Helper problem because this mongoid specific answer also works. Choose your poison.

My understanding here is that the WillPaginate module injects itself directly into Sinatra's Helpers module (a monkey patch). In certain environments, like mine, you have to extend this into Sinatra by registering it. Hope this saves someone some time.