Little Live search problem

Works fine but when you use backspace or delete on the search word and
it hits 0 characters it displays ALL the Test names in the database!

def live_search
@phrase = request.raw_post || request.query_string
a1 = “%”
a2 = “%”
@searchphrase = a1 + @phrase + a2
@results = Test.find(:all, :conditions => [ “test_name LIKE ?”,
@searchphrase])

@number_match = @results

render(:layout => false)

end

Rob B. wrote:

Works fine but when you use backspace or delete on the search word and
it hits 0 characters it displays ALL the Test names in the database!

def live_search
@phrase = request.raw_post || request.query_string
a1 = “%”
a2 = “%”
@searchphrase = a1 + @phrase + a2
@results = Test.find(:all, :conditions => [ “test_name LIKE ?”,
@searchphrase])

@number_match = @results

render(:layout => false)

end

@results = @phrase.empty? ? nil : Test.find(:all, :conditions =>
[“test_name LIKE ?”, @searchphrase])

Alex W. wrote:

@results = @phrase.empty? ? nil : Test.find(:all, :conditions =>
[“test_name LIKE ?”, @searchphrase])

Thanks Alex but I have .empty? in my view for the results.
Any way around this?

NoMethodError in Search#live_search

Showing app/views/search/live_search.rhtml where line #1 raised:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.empty?

Extracted source (around line #1):

1: <% if @results.empty? %>
2: No match for ‘<%=h @phrase %>.’
3: <% else %>
4: <% @results.each do |result| %>

Justin Mecham wrote:

Try @phrase.blank? instead, as it will check for a nil value before
attempting to call empty?.

J

Didn’t work, same result as above.

Hang on just swapped the view to say .blank? too and it works. Thanks J
and Alex.

Try @phrase.blank? instead, as it will check for a nil value before
attempting to call empty?.

J