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 -- Posted via http://www.ruby-forum.com/.
Rob Balfour 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]) -- Posted via http://www.ruby-forum.com/.
Alex Wayne 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| %> -- Posted via http://www.ruby-forum.com/.
Try @phrase.blank? instead, as it will check for a nil value before attempting to call empty?. J On Jun 7, 2006, at 11:07 AM, Rob Balfour wrote:> Alex Wayne wrote: >> @results = @phrase.empty? ? nil : Test.find(:all, :conditions => >> ["test_name LIKE ?", @searchphrase]) > > 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?. > > JDidn''t work, same result as above. -- Posted via http://www.ruby-forum.com/.