Rails 修剪 searchlogic 文本字段中的空白

发布于 2024-09-12 04:29:33 字数 467 浏览 8 评论 0原文

这有效:

基线控制器

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看春风乍起 2024-09-19 04:29:33

令人惊讶的是,尼克很难找到有关高级 Searchlogic 技术的信息。消毒对我来说特别困难。

这是解决您的问题的一种非常巧妙(且快速)的方法。

控制器

保持相同的

views/baselines/index.html.erb

<% form_for @search do |f| %>
  <%= f.text_field :keywords %>
  <%= submit_tag 'Search' %>
<% end %>

models/baseline.rb

class Baseline < ActiveRecord::Base
  scope_procedure :keywords, lambda { |query|
    baseline_name_like_or_description_like(query.strip)
  }
end

Extras

我很高兴能分享我在Searchlogic 中学到的其他很酷的东西,所以我将在这里分享它们。

首先,只需很少的工作,您就可以用最少的努力来启动关键字scope_procedure。

scope_procedure :keywords, lambda { |query|
  baseline_name_like_any_or_description_like_any(query.strip.split(/\s+/))
}

注意向每个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

class BaselineSearch < Searchlogic::Search
  def initialize(params, klass, current_scope)

    allowed_params = [:keywords, :name_like_any, :foo_equals, :order]

    conditions = {}

    for x in allowed_params 
      conditions[x] = params[x] unless params[x].blank?
    end

    super(klass, current_scope, conditions)
  end
end

如果您想知道我在哪里找到 initialize 方法签名,请检查 Searchlogic::Search

现在,您需要创建一个简单的覆盖,而不是在模型上调用 Searchlogic::Search在你的baseline.rb中。在这里,我们将实现我们自己的 Searchlogic::Search::Implementation

models/baseline.rb

class Baseline < ActiveRecord::Base

  # cool scope procedures
  # ...

  def self.search(params={})
    BaselineSearch.new(params || {}, self, scope(:find))
  end
end

现在,当您调用 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

<% form_for @search do |f| %>
  <%= f.text_field :keywords %>
  <%= submit_tag 'Search' %>
<% end %>

models/baseline.rb

class Baseline < ActiveRecord::Base
  scope_procedure :keywords, lambda { |query|
    baseline_name_like_or_description_like(query.strip)
  }
end

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.

scope_procedure :keywords, lambda { |query|
  baseline_name_like_any_or_description_like_any(query.strip.split(/\s+/))
}

Note the addition of the any operator to each named_scope

This 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 predefined named_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

class BaselineSearch < Searchlogic::Search
  def initialize(params, klass, current_scope)

    allowed_params = [:keywords, :name_like_any, :foo_equals, :order]

    conditions = {}

    for x in allowed_params 
      conditions[x] = params[x] unless params[x].blank?
    end

    super(klass, current_scope, conditions)
  end
end

If you're wondering where I found that initialize method signature, check Searchlogic::Search

Now, 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

class Baseline < ActiveRecord::Base

  # cool scope procedures
  # ...

  def self.search(params={})
    BaselineSearch.new(params || {}, self, scope(:find))
  end
end

Now, when you call Baseline.search(params[:search]), it will invoke a new BaselineSearch instead of the Searchlogic::Search default. The cool thing here is, if you want to skip using your BaselineSearch, you can call Baseline.searchlogic(params[:search]) to use the Searchlogic default instead.

时光清浅 2024-09-19 04:29:33

在你的控制器中:

params[:search][:baseline_name_like_or_description_like].strip!

In your controller:

params[:search][:baseline_name_like_or_description_like].strip!
不气馁 2024-09-19 04:29:33

或者,要删除应用程序中的所有搜索,请将其放入 ApplicationController

before_filter :strip_search_parameters      

def strip_search_parameters
  return if params[:search].blank?
  params[:search].each_value {|val| val.strip!}
  return
end

Or for strip all searches in your app, put this into ApplicationController

before_filter :strip_search_parameters      

def strip_search_parameters
  return if params[:search].blank?
  params[:search].each_value {|val| val.strip!}
  return
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文