ActiveAdmin - 使用默认值过滤

Ton*_*shi 14 activeadmin

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

filter  :country, :default=>'US'
Run Code Online (Sandbox Code Playgroud)

Fiv*_*ell 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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

  • 此代码段存在在范围之间切换时清除所有用户过滤器的问题。 (2认同)
  • 从 Rails 5 开始是 before_action (2认同)

Ivo*_*cet 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
Run Code Online (Sandbox Code Playgroud)

  • before_filter 已弃用,应使用 before_action 代替。/sf/answers/1156388901/ (2认同)