Rails - Ransack - 自定义attribute_select

时间:2012-11-07 18:54:42

标签: ruby-on-rails ransack

我在我的Rails应用程序中使用ransack gem进行高级搜索。如何过滤attribute_select方法以不显示某些字段,例如idcreated_at等。

= f.attribute_fields do |a|
  = a.attribute_select

我上面的当前方法只列出了所有属性。

2 个答案:

答案 0 :(得分:4)

根据Why your Ruby class macros (might) suck (mine did)

def self.ransackable_attributes(auth_object = nil)
  super - ['id', 'created_at']
end

答案 1 :(得分:1)

您还可以定义可搜索的属性,而不是指定不可搜索的属性,如下所示:

def self.ransackable_attributes(auth_object = nil)
    %w( searchable_attribute_goes_here another_one_goes_here ... ) + _ransackers.keys
end