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?
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
keywordsscope_procedure with minimal effort.Note the addition of the
anyoperator 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_procedureinstead of a predefinednamed_scopein 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
initializemethod 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 newBaselineSearchinstead 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.