Ruby on Rails - 活动管理员奇怪的错误

时间:2014-04-07 16:53:27

标签: ruby-on-rails activeadmin

On Rails 4.当我尝试在Active Admin中加载索引页面时,我遇到了一些奇怪的错误。他们之前都工作正常,但突然我开始收到这条消息(在这个例子中我加载了我的类别索引,但大多数情况都是这样):

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'

我搜索了该方法名称,它只返回了这个:

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

我可以很好地查看仪表板和各个行页面,但是当我去编辑记录时,我得到了这个:

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'

我不知道这意味着什么...重新安装Active Admin /重新生成资产会更好吗?这样做是否安全?如果是这样,我该怎么做?或者,是否有这些错误消息的简单修复。谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题并为您解决了问题。问题是,自动向所需字段添加CSS *的聪明initializer与ActiveAdmin使用的Ransack搜索不兼容。解决方案是在调用之前检查模型responds_to是否为必要的方法:

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

答案 1 :(得分:1)

AA适用于Rails 4和Ruby 2.1.1,但是,您必须从Github的主分支中获取AA。请注意,AA从" meta_search"宝石到"洗劫" API兼容 - 所以有些事情肯定会破坏。

也许您使用自定义过滤器?由于基于范围的自定义过滤器,我遇到了您的错误,因为" ransack"没有来自" meta_search"的search_method之类的内容。以下是我如何解决这个问题,以防万一: