title: In Rails, how do I prevent my search function from automatically running a blank search on page load?
body: <p>I have a controller that has a search function. The search function displays  an alert message it a user doesn't enter anything into the search field. The problem is that this message displays when the page loads - before submit has been clicked. How do I prevent this?</p>
  
  <p>Here is my controller code:</p>
  
  <pre><code>class LocationsController &lt; ApplicationController
    before_action :authenticate_user!
  
    def index
      if params[:q].blank?
        @message = 'Please enter an address in the field!'
        return
      end
  
      token = Rails.application.credentials.locationiq_key
      search = LocationiqApi.new(token).find_place(params[:q])
  
      # Hash#dig will return nil if ANY part of the lookup fails
      latitude = search.dig('searchresults', 'place', 'lat')
      longitude = search.dig('searchresults', 'place', 'lon')
  
      if latitude.nil? || longitude.nil?
        # Output an error message if lat or lon is nil
        @coordinates = "We couldn't find a place by this name. Please enter a valid place name."
      else
        @coordinates = "Coordinates: " + "#{latitude}, #{longitude}"
      end
    end
  end
  </code></pre>
  
  <p>and my view:</p>
  
  <pre><code>&lt;main&gt;
    &lt;h1&gt;Location Search&lt;/h1&gt;
    &lt;!-- devise flash messages --&gt;
    &lt;p class="notice"&gt;&lt;%= notice %&gt;&lt;/p&gt;
    &lt;p class="alert"&gt;&lt;%= alert %&gt;&lt;/p&gt;
    &lt;!-- Button to search coordinates --&gt;
    &lt;%= form_tag(locations_path, method: :get) do %&gt;
      &lt;%= text_field_tag(:q) %&gt;
      &lt;%= submit_tag("geocode") %&gt;
      &lt;%= @message %&gt;
    &lt;% end %&gt;&lt;br&gt;
  
    &lt;%= @coordinates %&gt;
  &lt;/main&gt;
  </code></pre>