15

想知道是否可以使用活动管理员使用默认值进行过滤?这将有助于为管理员用户预加载数据。

filter  :country, :default=>'US'
4

2 回答 2

22

您可以通过定义 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

升级版:

在某些情况下,如果 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
于 2012-12-17T00:12:25.623 回答
9

改编后的 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
于 2016-04-29T13:47:55.727 回答