Anybody know how to create a search bar
You should take a look at acts_as_ferret:
http://projects.jkraemer.net/acts_as_ferret/
There’s also a whole demo Rails project (containing a simple model
class, scaffolded CRUD GUI and a search form) available at
svn://projects.jkraemer.net/acts_as_ferret/trunk/demo
If you’re just looking for a simple search you could do something like
this:
In your controller:
def search
@query = params[:query]
@search_results = MyModel.search(@query)
…
end
In your model:
def self.search(query)
if !query.to_s.strip.empty?
tokens = query.split.collect {|c| “%#{c.downcase}%”}
find_by_sql([“SELECT * from your_table WHERE #{
([”(LOWER(your_column_to_search) LIKE ? OR
LOWER(another_column_to_search) LIKE ?)"] * tokens.size).join(" AND “) }
ORDER by some_column DESC”, *tokens.collect { |token| [token] * 3
}.flatten])
else
…
end
end
Thanks Lady,
Ill try to be more specific in the future + I do my own work, but I like
to take ideas from others. Thats why forums exist, so people can share.
rookie wrote:
Anybody know how to create a search bar
I’ll be nice enough and respond to this;
- Be more specific about what it is you want to do
- Describe the exact issue you’re facing / struggling with
- Don’t expect people to do your work for you - show that you atleast
tried.
It’s called netiquette.
Thanks.
Kind regards,
Henning K.