Rails 4 / Filterrific gem - 布尔字段问题
Rails 4 / Filterrific gem - Problem with boolean field
在我的应用程序中,我有一个名为 tested
的字段,它是一个 boolean
字段。
我想要实现的是一个简单的 checkbox
,用户可以根据 tested
.
选择或取消选择和过滤
在我的模型中我有:
filterrific :default_filter_params => { :sorted_by => 'created_at_desc' },
:available_filters => %w[
sorted_by
search_query
with_created_at_gte
with_tested
]
scope :with_tested, lambda { |flag|
return nil if 0 == flag # checkbox unchecked
where(tested: true)
}
/// Other scopes
并且在我的 view/form 中我有:
= f.check_box :with_tested
在我的模型中,我也尝试了不同的方法但没有成功:
scope :with_tested, lambda { |value|
where('posts.tested = ?', value)
}
// and
scope :with_tested, lambda { |query|
return nil if 0 == query # checkbox unchecked
where('posts.tested == ?', query)
}
// and
scope :with_tested, lambda { |flag|
return nil if 0 == flag # checkbox unchecked
where(tested: [flag])
}
当我尝试基于 tested
进行过滤时,我可以看到我的过滤器正在尝试过滤 (我看到过滤器旋转),但我的记录是过滤不正确。
我不确定我做错了什么。感谢任何建议和帮助!
过滤器的所有其他部分工作正常
PS:我没有在我的控制器中添加 with_tested
因为我知道我不需要它
版本:
Ruby 在 Rails 上:4.2.4
极好的滤镜:2.1.2
问题是where(tested: [flag])
,因为它还没有设置到true
或false
。要解决此问题 where
应该 知道 value
在 那种情况下 .
是什么
所以where(tested: [flag])
应该改为:where(tested: true)
或where(tested: false)
.
首先,您需要在 model
:
中指定
scope :with_tested, lambda { |flag|
return nil if 0 == flag # checkbox unchecked
where(tested: true)
}
在您的 view
中执行以下操作:
= f.check_box :with_tested
并在 available_filters
的控制器中添加 :with_tested
。
PS:代码已经过测试并且有效
在我的应用程序中,我有一个名为 tested
的字段,它是一个 boolean
字段。
我想要实现的是一个简单的 checkbox
,用户可以根据 tested
.
在我的模型中我有:
filterrific :default_filter_params => { :sorted_by => 'created_at_desc' },
:available_filters => %w[
sorted_by
search_query
with_created_at_gte
with_tested
]
scope :with_tested, lambda { |flag|
return nil if 0 == flag # checkbox unchecked
where(tested: true)
}
/// Other scopes
并且在我的 view/form 中我有:
= f.check_box :with_tested
在我的模型中,我也尝试了不同的方法但没有成功:
scope :with_tested, lambda { |value|
where('posts.tested = ?', value)
}
// and
scope :with_tested, lambda { |query|
return nil if 0 == query # checkbox unchecked
where('posts.tested == ?', query)
}
// and
scope :with_tested, lambda { |flag|
return nil if 0 == flag # checkbox unchecked
where(tested: [flag])
}
当我尝试基于 tested
进行过滤时,我可以看到我的过滤器正在尝试过滤 (我看到过滤器旋转),但我的记录是过滤不正确。
我不确定我做错了什么。感谢任何建议和帮助!
过滤器的所有其他部分工作正常
PS:我没有在我的控制器中添加 with_tested
因为我知道我不需要它
版本:
Ruby 在 Rails 上:4.2.4
极好的滤镜:2.1.2
问题是where(tested: [flag])
,因为它还没有设置到true
或false
。要解决此问题 where
应该 知道 value
在 那种情况下 .
所以where(tested: [flag])
应该改为:where(tested: true)
或where(tested: false)
.
首先,您需要在 model
:
scope :with_tested, lambda { |flag|
return nil if 0 == flag # checkbox unchecked
where(tested: true)
}
在您的 view
中执行以下操作:
= f.check_box :with_tested
并在 available_filters
的控制器中添加 :with_tested
。
PS:代码已经过测试并且有效