title: 500 internal server error in rails with ajax
body: <p>I am trying to make a text box that displays a list of sorted users that sorts per every typed letter. From there the person can add the user as a collaborator. The result should be kind of like facebook's search feature when you are searching to add new friends. When I press a key I see a new 500 internal server error in the network section of the browsers console. Here is a snippet of the response:</p>
  
  <pre><code> &lt;h1&gt;
      NoMethodError
        in CollaborationsController#collaboration_search
    &lt;/h1&gt;
  &lt;/header&gt;
  
  &lt;div id="container"&gt;
    &lt;h2&gt;undefined method `[]&amp;#39; for nil:NilClass&lt;/h2&gt;
  </code></pre>
  
  <p>So I think the ajax is getting fired to the server but there is something wrong with the controller. Here is my code for the view, views/collaborations/_new.html.erb:</p>
  
  <pre><code>    &lt;%= form_for [wiki, collaboration] do |f|%&gt;
  
  &lt;div class = "col-md-8"&gt;
  
      &lt;div class = "form-group"&gt;
        &lt;%= f.label :user_name %&gt;
        &lt;%= f.text_field :user_name, class: 'form-control', placeholder: "Enter name"  %&gt;
  
      &lt;/div&gt;
      &lt;div class = "form-group"&gt;
        &lt;%= f.submit class: 'btn btn-success' %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;%= @users_by_name.to_a %&gt;
  &lt;/div&gt;
  
    &lt;/div&gt;
  
  &lt;/div&gt;
  &lt;%end%&gt;
  </code></pre>
  
  <p>where the form above is a partial. The ajax is written in javascripts/collaborations.js:
      $(document).ready(function()
  {
   // alert(1);</p>
  
  <pre><code> $('#collaboration_user_name').on('keyup', function() { 
  
        // text = $(this).val();
        // alert(text);
  
         $.ajax({ url: "/collaborations", 
         beforeSend: function( xhr ) { 
          xhr.overrideMimeType( "text/plain; charset=x-user-defined" ); 
        } 
       }).done(function( data ) { 
        if( console &amp;&amp; console.log ) { 
          console.log( "Sample of data:", data.slice( 0, 100 ) ); 
          //alert()
  
        } 
      });
   });
  });
  </code></pre>
  
  <p>Here is the collaboration#search_collaboration action within the collaborations controller:</p>
  
  <pre><code> def collaboration_search
    name_search = params[:collaboration][:user_name].to_s
    @users_by_name = User.where('name Like ?', "%#{params[:collaboration][:user_name]}%")
     render json: @users_by_name.map do |user|
       { name: user.name, id: user.id}
    end
  </code></pre>
  
  <p>end</p>
  
  <p>And just for testing purposes I kept my collaborations/search_collaboration.js.erb very simple:</p>
  
  <pre><code>alert('hello');
  </code></pre>
  
  <p>however if someone can point me in the right direction for how to list the names of the users returned from the collaboration_search action that would be much appreciated!</p>