let autocomplete fail gracefully when param is not set

This commit is contained in:
Reinier Balt 2011-01-10 22:47:19 +01:00
parent 05ba6060fc
commit 1a821a228f
3 changed files with 13 additions and 10 deletions

View file

@ -128,9 +128,13 @@ class ApplicationController < ActionController::Base
end
def for_autocomplete(coll, substr)
filtered = coll.find_all{|item| item.name.downcase.include? substr.downcase}
json_elems = "[{" + filtered.map {|item| "\"value\" : \"#{item.name}\", \"id\" : \"#{item.id}\""}.join("},{") + "}]"
return json_elems == "[{}]" ? "" : json_elems
if substr # protect agains empty request
filtered = coll.find_all{|item| item.name.downcase.include? substr.downcase}
json_elems = "[{" + filtered.map {|item| "\"value\" : \"#{item.name}\", \"id\" : \"#{item.id}\""}.join("},{") + "}]"
return json_elems == "[{}]" ? "" : json_elems
else
return ""
end
end
# Uses RedCloth to transform text using either Textile or Markdown Need to