简体   繁体   English

Ruby on Rails filterrific宝石简单过滤器

[英]Ruby on Rails filterrific gem simple filter

I'm new to Ruby on Rails and currently trying to set up an easy search with filterrific. 我是Ruby on Rails的新手,目前正在尝试使用filterrific设置简单的搜索。 But all examples I can find are much more complicated then mine and I'm a bit confused: 但是我能找到的所有例子都比我的复杂得多,我有些困惑:

I have a model where I store Bank Holidays. 我有一个存储银行假期的模型。 I want to filter the output so the user just sees one year, should default to current year. 我想过滤输出,以便用户只看到一年,应该默认为当前年份。

Therefore I created a scope in my model: 因此,我在模型中创建了一个范围:

  selected_year = Date.today.year
  scope :year, -> {
  where("YEAR(holiday_date) = #{selected_year}")
  }

All examples for filterrific have found exspect that a user wants to search matching different values in related databases. 所有关于filterrific的示例都发现用户希望在相关数据库中搜索匹配不同值的示例。

Any hint on how to filter just simple one value? 关于如何仅过滤一个简单值的任何提示?

Thanks Simon 谢谢西蒙

It looks like you want to allow the scope to receive an argument like so 看起来您想允许范围接收这样的参数

scope :year, ->(year) { where("YEAR(holiday_date) = #{year}") }

Then you can call Model.year(1986) etc. 然后可以调用Model.year(1986)等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM