How to refactor this complicated html generator in Ruby?
This needs some tweaking, but I hope it helps you identify the main issues to address in your code (IMHO):

objects = [:content, :answers, :comments].flat_map do |type|
  questions = Question.search_tank('', {
    :page =&gt; 1, 
    :per_page =&gt; 5,
    :conditions =&gt; conditions.merge(type =&gt; search_term), 
  })
  questions.map { |q| [q, type] }
end.uniq_by(&amp;:first).take(MAX_RESULTS)

results = objects.map do |q, type| 
  label = case type
  when :content
    highlight_matched(search_regex, q.content) + 
      content_tag(:span, "Question", :class =&gt; "search-type")
  when :answers
    content_tag(:span, "Answer", :class =&gt; 'search-type snippet'&gt;) + 
      [q.content, get_snippet(search_regex, q.answers)].compact.join(tag(:br))
  when :comments
    comments = get_snippet(search_regex, q.answers.flat_map(&amp;:comments))]    
    content_tag(:span, "Comment", :class =&gt; 'search-type snippet'&gt;) +
      [t, q.content, comments].compact.join(tag(:br))
  end
  {:title =&gt; q.content, :value =&gt; app_question_url(q.app, q.id), :label =&gt; label}
end


Notes:


Avoid stateful variables whenever possible (that's it, don't update inplace), work with expressions. 
Use Rails helpers (content_tag, tag, ...).
Use symbols for key-like values, not strings.
"#{something}" -> something.to_s
map + flatten = flat_map. Even though you should able to write q.answer_commments in Rails 3.1 will the correct model settings.
Use case, not a chain of if/elsif if the condition is on the same value.