Sinatra and Kaminari without any views and without padrino-helpers gem

If you have an app that doesn't have any views (for example it only responds with JSON) you still may want to use excellent Kaminari gem for paginating big data sets. Unfortunately even when you work only with JSON and you don't need any views helpers, you will still get this warning:

[!]You should install `padrino-helpers' gem if you want
to use kaminari's pagination helpers with Sinatra.
[!]Kaminari::Helpers::SinatraHelper does nothing now...

so to get rid of this, you have to add:

gem 'padrino-helpers'

to your Gemfile (even when you don't need it).

Luckily, there's a better way to do this. Add Kaminari to your Gemfile that way:

gem 'kaminari', require: %w( kaminari kaminari/hooks )

and then, either create a config/initializers/kaminari.rb or directly in your app.rb put this:

# Initialize the Kaminari gem
::Kaminari::Hooks.init

This will initialize Kaminari without requesting padrino-helpers.

Categories: Ruby, Software

2 Comments

  1. I’ve been looking all over for this solution! This is also how you use Kaminari in a basic ActiveRecord dependent gem that does not have Rails. Thank you!!

  2. Maciej Mensfeld

    December 30, 2016 — 10:15

    You’re welcome. It is crazy how 2-3 years old posts can be still helpful :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑