Running with Ruby

Tag: Pagination (page 1 of 3)

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.

Rails 4.0: ActionView::Template::Error: undefined method paginate with Kaminari

Refactoring can be sometimes a pain in the ass. Especially when you’re refactoring things that are really old. Today I was cleaning an almost 5 years old piece of code. It was a Rails 4.0 (migrated many times from 1.2) app that had helpers auto-loading turned on. I hated this, because it forced us to use names with prefixes for many methods.

Disabling it was really easy (in config/application.rb):

config.action_controller.include_all_helpers = false

Of course after that you need to require all the helpers that you use:

helper ApplicationHelper
helper FormHelper

Everything was fine except one small detail:

ActionView::Template::Error: undefined method `paginate' for #<#<Class:0x0000000929a3c0>:0x0000000aa2c5b0>
    app/views/admin/module/urls/index.html.haml:1:in `_app_views_admin_module_urls_index_html_haml__883223636925488268_77061720'
    app/controllers/application_controller.rb:57:in `respond_with'
    app/controllers/admin/module/urls_controller.rb:9:in `index'
    test/controllers/admin/module/urls_controller_test.rb:103:in `block in <class:UrlsControllerTest>'

Kaminari stopped working. It seems that it was loaded without issues, except the helper. To fix this, just include in you application controller following code:

helper KaminariHelper
Olderposts

Copyright © 2018 Running with Ruby

Theme by Anders NorenUp ↑