Rachel9494
Rachel9494

Reputation: 824

Ruby on Rails - Active Admin Strange Errors

On Rails 4. I'm getting some strange errors when I try to load index pages in Active Admin. They were all working fine before, but suddenly I started getting this message (for this example I loaded my Categories index but it is happening for most of them):

NoMethodError in Admin::Categories#index
Showing c:/Ruby200/lib/ruby/gems/2.0.0/bundler/gems/active_admin-3fb7f03335b1/app/views/active_admin/resource/index.html.arb where line #1 raised:

undefined method `validators_on' for Ransack::Search:Class
Extracted source (around line #1):

  insert_tag renderer_for(:index)

Application Trace | Framework Trace | Full Trace
config/initializers/form_builder.rb:12:in `label'

I did a search for that method name and it returned only this:

In form_builder.rb

if object.class.validators_on(method).map(&:class).include? ActiveRecord::Validations::PresenceValidator
      if options.class != Hash
        options = {:class => "required"}
      else
        options[:class] = ((options[:class] || "") + " required").split(" ").uniq.join(" ")
      end
    end

I can view the dashboard and individual row pages fine, but when I go to edit a record I get this:

TypeError in Admin::Categories#edit
Showing c:/Ruby200/lib/ruby/gems/2.0.0/bundler/gems/active_admin-3fb7f03335b1/app/views/active_admin/resource/edit.html.arb where line #1 raised:

no implicit conversion of String into Array
Extracted source (around line #1):

  insert_tag renderer_for(:edit)

Application Trace | Framework Trace | Full Trace
config/initializers/form_builder.rb:16:in `label'

I have no idea what this means...would it be better to re-install Active Admin/regenerate its assets? Is that a safe thing to do? If so, how do I do that? Or, is there a simple fix to these error messages. Thanks for any help.

Upvotes: 0

Views: 2250

Answers (2)

Dan Kohn
Dan Kohn

Reputation: 34347

I encountered the same issue and have a fix for you. The issue is that the very clever initializer that automatically adds a CSS * to required fields is not compatible with the Ransack search that ActiveAdmin uses. The solution is to check that the model responds_to the necessary method before invoking it:

class ActionView::Helpers::FormBuilder
  # http://blog.pothoven.net/2012/10/self-marking-required-fields-in-rails.html
  alias_method :orig_label, :label

  # add a 'required' CSS class to the field label if the field is required
  def label(method, content_or_options = nil, options = nil, &block)
    if content_or_options && content_or_options.class == Hash
      options = content_or_options
    else
      content = content_or_options
    end

    options = add_required_class(options) if presence_required?(method)
    orig_label(method, content, options || {}, &block)
  end

  private

  def add_required_class(options)
    return { class: 'required' } unless options.class == Hash
    new_class = ((options[:class].to_s || '') + ' required')
      .split(' ').uniq.join(' ')
    options.merge!(class: new_class)
  end

  def presence_required?(method)
    object.class.respond_to?(:validators_on) &&
      object.class.validators_on(method).collect(&:class)
        .include?(ActiveRecord::Validations::PresenceValidator)
  end
end

Upvotes: 2

svoop
svoop

Reputation: 3464

AA works with Rails 4 and Ruby 2.1.1, however, you have to take AA from the master branch at Github. Please note that AA switched from the "meta_search" gem to "ransack" which is not API-compatible - so some things are sure to break.

Maybe you use custom filters? I had your kind of errors due to custom filters based on scopes since "ransack" does not feature anything like search_method from "meta_search". Here's how I work around this, just in case:

Upvotes: 1

Related Questions