Category: Rails

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

Ransack – no translations for ActiveRecord nested models

Ransack is a great gem. You can consider it as a new better (with sparkles!) version of MetaSearch. Unfortunately, it seems that it is not translating ActiveRecord attributes, when they are nested.
For example:

module System
  class Setting < ActiveRecord::Base
    validates :name,
      presence: true
  end
end

We have a name attribute on a System::Setting model. According to Rails doc translation yaml for such a structure should be similar to this one:

pl:
  activerecord:
    attributes:
      "system/setting":
        name: Nazwa

Unluckily this won't work (it did with MetaSearch). When you look into Ransack /lib/ransack/translate.rb file, you will find such a piece of code (around line 25):

defaults = base_ancestors.map do |klass|
  :"ransack.attributes.#{klass.model_name.singular}.#{original_name}"
end

You can see, that Ransack is enclosing all attributes in it's own namespace. I can't do this, since I have gems that use "original" Rails convention (activerecord.attributes), so I had to replace those lines with:

defaults  = []
base_ancestors.map do |klass|
  defaults << :"activerecord.attributes.#{klass.model_name.i18n_key}.#{original_name}"
  defaults << :"ransack.attributes.#{klass.model_name.singular}.#{original_name}"
end

After that, Ransack is supporting both namespaces. The whole translate file that you should include in your project should look like this:

I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'locale', '*.yml')]

# This is a temporary workaround for this issue:
# https://github.com/ernie/ransack/issues/273
# It allows names to be translated in sort_link @search, :name based on their
# activerecord.attributes scope
module Ransack
  module Translate
    def self.attribute(key, options = {})
      unless context = options.delete(:context)
        raise ArgumentError, "A context is required to translate attributes"
      end

      original_name = key.to_s
      base_class = context.klass
      base_ancestors = base_class.ancestors.select { |x| x.respond_to?(:model_name) }
      predicate = Predicate.detect_from_string(original_name)
      attributes_str = original_name.sub(/_#{predicate}$/, '')
      attribute_names = attributes_str.split(/_and_|_or_/)
      combinator = attributes_str.match(/_and_/) ? :and : :or
      defaults  = []
      base_ancestors.map do |klass|
        defaults << :"activerecord.attributes.#{klass.model_name.i18n_key}.#{original_name}"
        defaults << :"ransack.attributes.#{klass.model_name.singular}.#{original_name}"
      end

      translated_names = attribute_names.map do |attr|
        attribute_name(context, attr, options[:include_associations])
      end

      interpolations = {}
      interpolations[:attributes] = translated_names.join(" #{Translate.word(combinator)} ")

      if predicate
        defaults << "%{attributes} %{predicate}"
        interpolations[:predicate] = Translate.predicate(predicate)
      else
        defaults << "%{attributes}"
      end

      defaults << options.delete(:default) if options[:default]
      options.reverse_merge! :count => 1, :default => defaults
      I18n.translate(defaults.shift, options.merge(interpolations))
    end

  end
end

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑