title: Spotify Search API, how to set query params to dynamically equal a users input in a search form
body: <p>I'm trying to build a search form that allows a user to search for an artist on Spotify via the Spotify API.  I am able to reach Spotify's data and search for an artist as long as the params[:query] is already defined but obviously I want params[:query] to be defined as whatever search input a user enters..  Very new to Rails, how do I do this?  What I have so far..</p>
  
  <p>Error message I receive when navigating to my artist search page (artists/index.html.erb) while params[:query] is not defined:</p>
  
  <pre><code>NoMethodError in ArtistsController#index
  undefined method `encoding' for nil:NilClass
  </code></pre>
  
  <p>problem line:</p>
  
  <p><code>response = HTTParty.get("https://api.spotify.com/v1/search?q=#{CGI.escape @query}&amp;type=artist")</code></p>
  
  <p>artists_controller.rb:</p>
  
  <pre><code>class ArtistsController &lt; ApplicationController
    def index
      response = HTTParty.get("https://api.spotify.com/v1/search?q=#{CGI.escape params[:query]}&amp;type=artist")
      @hash_version = JSON.parse(response.body)
    end
  end
  </code></pre>
  
  <p>My search form and how I'm searching for artists</p>
  
  <p>artists/index.html.erb:</p>
  
  <pre><code>&lt;h1&gt;Artist Search&lt;/h1&gt;
  &lt;i&gt;(search for artists you would like to follow)&lt;/i&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;%= form_tag("/search", method: "get") do %&gt;
    &lt;div class="form-group"&gt;
      &lt;%= label_tag(:query, "Search artist by name:") %&gt;
      &lt;%= text_field_tag(:query, "", class: 'form-control', autofocus: true, placeholder: "Enter artist name") %&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;%= submit_tag("Search", class: 'btn btn-success') %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  
  &lt;% @hash_version["artists"]["items"].each_with_index do |band, index| %&gt;
    &lt;% if index == @hash_version["artists"]["items"].size - 1 %&gt;
      &lt;li class="artist_li"&gt;
        &lt;%= image_tag(band["images"].first["url"], class: 'img-responsive artist_img') rescue image_tag("microphone.png", class: 'img-responsive artist_img mic_bg') %&gt;
        &lt;br&gt;
        &lt;h1 class="artist_name"&gt;&lt;%= band["name"] %&gt;&lt;/h1&gt;
        &lt;br&gt;
        &lt;% if band["genres"].empty? %&gt;
          &lt;h5&gt;no genres&lt;/h5&gt;
        &lt;% else %&gt;
          &lt;h5&gt;&lt;%= band["genres"] %&gt;&lt;/h5&gt;
        &lt;% end %&gt;
        &lt;br&gt;
        &lt;h5&gt;&lt;%= band["followers"]["total"] %&gt; followers&lt;/h5&gt;
        &lt;br&gt;
        &lt;h4&gt;(Follow)&lt;/h4&gt;
        &lt;!-- no hr with last li --&gt;
      &lt;/li&gt;
    &lt;% else %&gt;
      &lt;li class="artist_li"&gt;
        &lt;%= image_tag(band["images"].first["url"], class: 'img-responsive artist_img') rescue image_tag("microphone.png", class: 'img-responsive artist_img mic_bg') %&gt;
        &lt;br&gt;
        &lt;h1 class="artist_name"&gt;&lt;%= band["name"] %&gt;&lt;/h1&gt;
        &lt;br&gt;
        &lt;% if band["genres"].empty? %&gt;
          &lt;h5&gt;no genres&lt;/h5&gt;
        &lt;% else %&gt;
          &lt;h5&gt;&lt;%= band["genres"] %&gt;&lt;/h5&gt;
        &lt;% end %&gt;
        &lt;br&gt;
        &lt;h5&gt;&lt;%= band["followers"]["total"] %&gt; followers&lt;/h5&gt;
        &lt;br&gt;
        &lt;h4&gt;(Follow)&lt;/h4&gt;
        &lt;hr class="artists_hr"&gt;
      &lt;/li&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>