【问题标题】:Rails - Ransack - Custom attribute_selectRails - Ransack - 自定义属性选择
【发布时间】:2012-10-27 21:29:09
【问题描述】:

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

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

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

【问题讨论】:

    标签: ruby-on-rails ransack


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2014-12-27
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-03
        • 2019-05-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多