My form
<% form_for @search do |f| %>
<%= f.input :name_like_any %>
...
&开发者_如何学编程lt;% end %>
Controller
@search = Product.search
@search.name_like_any(params[:search][:name_like_any].split(/\s+/))
@products = search.all
This returns the correct result, but now my form shows the name as ["foo", "bar"]
instead of what the user input ("foo bar"
).
What's the elegant way to handle this?
Appreciate any feedback
Solution
Well, I found out the hard way first, then by asking another question, I inadvertently found a better answer to my original question. Here's the secondary question.
Model
# app/models/product.rb
class Product < ActiveRecord::Base
scope_procedure :keywords, lambda { |query|
name_like_any(query.split(/\s+/))
}
end
Controller
# app/controllers/products_controller.rb
class ProductsController < ApplicationController
def index
@search = Product.search(params[:search])
@products = @search.all
end
end
Views
# app/views/products/index.html.erb
<% form_for @search do |f| %>
<%= f.label :keywords, "Quick Search" %>
<%= f.input :keywords %>
<%= f.submit, "Go" %>
<% end %>
Stay tuned...
I'm having difficulty rallying up some of the more hard-to-answer questions for Searchlogic 2.x, but because tasks aren't always so straightforward, other questions tend to surface. Here's one I hope to answer that's not covered here.
How to sanitize form params for use with Searchlogic?
精彩评论