Rails 修剪 searchlogic 文本字段中的空白
这有效:
基线控制器
@search = Baseline.search(params[:search])
@baselines = @search.paginate :page => params[:page], :per_page => params[:per_page]
基线索引视图
<% form_for @search do |f| %>
<%= f.text_field :baseline_name_like_or_description_like %>
<%= submit_tag 'Search' %>
<% end %>
在哪里修剪 text_field
中的前导和尾随空格?我可以在某处使用 .strip!
吗?
This works:
Baseline Controller
@search = Baseline.search(params[:search])
@baselines = @search.paginate :page => params[:page], :per_page => params[:per_page]
baseline index view
<% form_for @search do |f| %>
<%= f.text_field :baseline_name_like_or_description_like %>
<%= submit_tag 'Search' %>
<% end %>
Where would I trim the leading and trailing whitespace in the text_field
? Could I use a .strip!
somewhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
令人惊讶的是,尼克很难找到有关高级 Searchlogic 技术的信息。消毒对我来说特别困难。
这是解决您的问题的一种非常巧妙(且快速)的方法。
控制器
保持相同的
views/baselines/index.html.erb
models/baseline.rb
Extras
我很高兴能分享我在Searchlogic 中学到的其他很酷的东西,所以我将在这里分享它们。
首先,只需很少的工作,您就可以用最少的努力来启动
关键字
scope_procedure。注意向每个named_scope添加
any
运算符这将允许您输入诸如“foo bar”之类的搜索,并且它将匹配诸如“i can foo haz bar”之类的baseline_names或“酒吧时间到了,foo!”如果名称是“foo”并且描述是“bar”,这甚至会匹配基线;重点是,如果您在 Searchlogic 表单中使用
scope_procedure
而不是预定义的named_scope
,您将获得大量额外的控制。其次,您可以付出一些额外的努力来清理您的搜索表单。这个问题花了相当长的时间才弄清楚,但我决定创建 Searchlogic::Search 类的子类。
查看:
models/baseline_search.rb
如果您想知道我在哪里找到
initialize
方法签名,请检查 Searchlogic::Search现在,您需要创建一个简单的覆盖,而不是在模型上调用 Searchlogic::Search在你的baseline.rb中。在这里,我们将实现我们自己的 Searchlogic::Search::Implementation
models/baseline.rb
现在,当您调用
Baseline.search(params[:search])
时,它将调用一个新的BaselineSearch
code> 而不是 Searchlogic::Search 默认值。这里很酷的一点是,如果您想跳过使用BaselineSearch
,您可以调用Baseline.searchlogic(params[:search])
来使用 Searchlogic 默认值。Nick, surprisingly, it's quite difficult to find information on advanced Searchlogic techniques. Sanitization has been particularly difficult for me to deal with.
Here's a pretty nifty (and quick) way to deal with your issue.
controller
stays the same
views/baselines/index.html.erb
models/baseline.rb
Extras
I'm excited to share the other cool things I've learned with Searchlogic, so I'll share them here.
First, with very little work, you can power-up that
keywords
scope_procedure with minimal effort.Note the addition of the
any
operator to each named_scopeThis will allow you to enter searches like "foo bar" and it will match baseline_names like "i can foo haz bar" or "bar time, foo!" This would even match a Baseline if the name was "foo" and the description was "bar"; point being, you get tons of extra control if you use
scope_procedure
instead of a predefinednamed_scope
in your Searchlogic forms.Second, you can sanitize your search forms with a little extra effort. This one took quite a while to figure out, but I decided to create a subclass of the Searchlogic::Search class.
Check it out:
models/baseline_search.rb
If you're wondering where I found that
initialize
method signature, check Searchlogic::SearchNow, instead of invoking Searchlogic::Search on your model, you need to create a simple override in your baseline.rb. Here, we'll implement our own Searchlogic::Search::Implementation
models/baseline.rb
Now, when you call
Baseline.search(params[:search])
, it will invoke a newBaselineSearch
instead of the Searchlogic::Search default. The cool thing here is, if you want to skip using yourBaselineSearch
, you can callBaseline.searchlogic(params[:search])
to use the Searchlogic default instead.在你的控制器中:
In your controller:
或者,要删除应用程序中的所有搜索,请将其放入 ApplicationController
Or for strip all searches in your app, put this into ApplicationController