Mah*_*tah 18 formtastic meta-search ruby-on-rails-3 activeadmin
我有一个带有名为"map_id"的整数列的表,我想添加一个activeadmin过滤器来过滤此列是否为NULL或IS NOT NULL.
怎么能实现呢?
我尝试了以下过滤器
filter :map_id, :label => 'Assigned', :as => :select, :collection => {:true => nil, :false => ''}
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误消息:
用于#的未定义方法`map_eq'
Avr*_*ore 27
如果有人在这个线程上发生了姗姗来迟,现在有一种简单的方法可以在活动管理中过滤null或非null:
filter :attribute_present, :as => :boolean
filter :attribute_blank, :as => :boolean
Run Code Online (Sandbox Code Playgroud)
不再需要向作用域添加自定义方法来完成此操作.
Gre*_*ret 13
没有找到一个好的解决方案,但这是一个如何.Active_admin的过滤器由meta_search完成,您可以覆盖模型中meta_search自动生成的函数以获得您想要的行为,最好的方法是使用范围,因为您需要返回一个关系以便与其他查询链接/范围,如规定在这里
在你的模型中:
for:as =>:选择过滤器,acitve_admin使用_eq wheres,这里是源代码
scope :map_eq,
lambda{ |id|
if(id !='none')
where( :map_id=> id)
else
where( :map_id=> nil)
end
}
#re-define the search method:
search_method :map_eq, :type => :integer
Run Code Online (Sandbox Code Playgroud)
在你的ative_admin寄存器块中:
filter :map_id, :label => 'Assigned', :as => :select, :collection => [['none', 'none'], ['one', 1],['tow', 2]]
# not using :none=>nil because active_admin will igore your nil value so your self-defined scope will never get chained.
Run Code Online (Sandbox Code Playgroud)
希望这有帮助.
似乎search_method在最近的rails版本中不起作用,这是另一个解决方案:
为您的模型添加范围:
scope :field_blank, -> { where "field is null" }
scope :field_not_blank, -> { where "field is not null" }
Run Code Online (Sandbox Code Playgroud)
添加到/ app/admin/[您的型号]
scope :field_blank
scope :field_not_blank
Run Code Online (Sandbox Code Playgroud)
您将看到这些范围的按钮出现(在顶部,模型名称下,而不是在过滤器部分)
归档时间: |
|
查看次数: |
6985 次 |
最近记录: |