title: Ruby/Rails Conditional search method (if ... else "no results found")
body: <p>Here is the code of a "simple search form" (thanks to jordinl) which I try to improve. I would like to add the case if there is no match found.</p>
  
  <p>So, there is the view (views/users/index.html.erb)</p>
  
  <pre><code>&lt;% form_tag users_path, :method =&gt; 'get' do %&gt;
    &lt;%= text_field_tag :search, params[:search] %&gt;
    &lt;%= submit_tag "Search", :name =&gt; nil %&gt;
  &lt;% end %&gt;
  
  &lt;% @users.each do |user| %&gt;  
    &lt;p&gt;&lt;%= link_to "#{user.name}", user %&gt;&lt;/p&gt;
  .
  .
  .
  &lt;% end %&gt;
  </code></pre>
  
  <p>The controller ( users_controller.rb)</p>
  
  <pre><code>def index
    @users = User.search(params[:search])
  end
  </code></pre>
  
  <p>and the method in user model:</p>
  
  <pre><code>def self.search(search)
    search.blank? ? [] : all(:conditions =&gt; ['name LIKE ?', "%#{search.strip}%"])
  end
  </code></pre>
  
  <p>I tried the following: </p>
  
  <pre><code>def self.search(search)
    if search.to_s.size &lt; 1
      []
    else
      if  @users.size &gt; 0
        all(:conditions =&gt; ['name LIKE ?', "%#{search.strip}%"])
      else
        render :text =&gt; "No result found"
      end
    end
  end
  </code></pre>
  
  <p>reporting the following error: "You have a nil object when you didn't expect it!..." (no instance in the array). Then, I tried to add </p>
  
  <pre><code>&lt;% if @users? %&gt;
    &lt;% @users.each do |user| %&gt;
  .
  .
  .
  </code></pre>
  
  <p>In the view. But it doesn't work either.</p>
  
  <p>I would be pleased to understand why I'm wrong on this. Thank you!</p>