ActiveAdmin - 使用默认值过滤

时间:2012-12-10 19:17:49

标签: activeadmin

想知道是否可以使用带有活动管理员的默认值过滤器?这对于为管理员用户预加载数据很有帮助。

filter  :country, :default=>'US'

2 个答案:

答案 0 :(得分:20)

您可以通过定义before_filter

来实现
  before_filter :only => [:index] do
    if params['commit'].blank?
       #country_contains or country_eq .. or depending of your filter type
       params['q'] = {:country_eq => 'US'} 
    end
  end

UPD:

在某些情况下,如果params [:q]为空或者params [:scope]为空,则需要设置过滤器

所以这可能效果更好

  before_filter :only => [:index] do
    if params['commit'].blank? && params['q'].blank? && params[:scope].blank?
       #country_contains or country_eq .. or depending of your filter type
       params['q'] = {:country_eq => 'US'} 
    end
  end

答案 1 :(得分:5)

改编的Fivells应答可以正常使用范围和下载。感到hacky但似乎做了这个工作。评论中的注释意图。

  before_filter only: :index do
    # when arriving through top navigation
    if params.keys == ["controller", "action"]
      extra_params = {"q" => {"country_eq" => "US"}}

      # make sure data is filtered and filters show correctly
      params.merge! extra_params

      # make sure downloads and scopes use the default filter
      request.query_parameters.merge! extra_params
    end
  end