title: How to filter with multiple parameters in Rails?
body: <p>I'm building an app in which users submit posts, then tag them with the year, location, and military unit they were in. On my index page, I'd like to have 3 dropdowns for year location and military unit, and when a user specifies different values in the dropdowns, the index filters through only posts with those parameters. However, I've failed over and over.</p>
  
  <p>Here's my Posts controller:</p>
  
  <pre><code>    def index
      @posts = Post.all
      @posts = @posts.location(params[:location]) if params[:location].present?
      @posts = @posts.unit(params[:unit]) if params[:unit].present?
      @posts = @posts.year(params[:year]) if params[:year].present?
      end
  </code></pre>
  
  <p>My index view:</p>
  
  <pre><code>    &lt;h1&gt;All Posts&lt;/h1&gt;
      &lt;%= form_tag 'search', method: 'get' do %&gt;
      &lt;%= select :location, options_for_select(countries) %&gt;
      &lt;%= select :year, Time.now.year.downto(1900).to_a %&gt;
      &lt;%= select :unit, options_for_select(["army", "navy"]) %&gt;
      &lt;%= submit_tag "filter" %&gt;
      &lt;% end %&gt;
  
  &lt;/p&gt;
  &lt;ul id="posts"&gt;
  &lt;% @posts.each do |post| %&gt;
  &lt;li&gt;
  &lt;%= link_to post.id, post_path(post) %&gt;
  &lt;/li&gt;
  &lt;% end %&gt;
  &lt;%= link_to "New Post", new_post_path %&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>And my Posts model:</p>
  
  <pre><code>    class Post &lt; ActiveRecord::Base
      belongs_to :user
      has_many :comments
      has_attached_file :image
      validates_attachment_content_type :image, :content_type =&gt;     ["image/jpg","image/jpeg","image/png"]
  end
  </code></pre>
  
  <p>With it like it is right now, the error I'm receiving is "NoMethodError in Posts#index" : "undefined method `empty?' for nil:NilClass". The highlighted line is from the view:</p>
  
  <pre><code>&lt;%= select :location_id, options_for_select(countries) %&gt;
  </code></pre>
  
  <p>Thanks for any help with this. I've searched and not found very much that has helped.</p>
  
  <p>EDIT: countries comes from the following helper:</p>
  
  <pre><code>    module CountriesHelper
  
  
      def countries
      [
       "Afghanistan",
       "Aland Islands",
       "Albania",
       "Algeria",
  
  #truncated for space
  
        "Western Sahara",
        "Yemen",
        "Zambia",
        "Zimbabwe"
      ]
  
      end
      end
  </code></pre>