title: Rails: Pass Params Through Ajax
body: <p>I need to pass params through javascript back to the server. At the moment, I pass them into javascript like so:</p>
  
  <pre><code>sendParams("&lt;%= params[:q].to_json %&gt;");
  </code></pre>
  
  <p>And then send them back like this:</p>
  
  <pre><code>function sendParams(q){
    $.ajax({
      url: '/mymodel/myaction',
      type: 'post',
      data: {'q':q},
      contentType: 'json'
    });
  }
  </code></pre>
  
  <p>In my controller, I try to use them like I would any other params:</p>
  
  <pre><code>MyModel.where(params[:q])
  </code></pre>
  
  <p>But the params are coming back empty, even though firebug shows this in the POST tab:</p>
  
  <pre><code>q=%7B%26quot%3Bc%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Ba%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Btitle%26quot%3B%7D%7D%2C%26quot%3Bp%26quot%3B%3A%26quot%3Bcont%26quot%3B%2C%26quot%3Bv%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bvalue%26quot%3B%3A%26quot%3B2%26quot%3B%7D%7D%7D%7D%2C%26quot%3Bs%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Bvotes_popularity%26quot%3B%2C%26quot%3Bdir%26quot%3B%3A%26quot%3Bdesc%26quot%3B%7D%7D%7D
  </code></pre>
  
  <p>Any idea why this information isn't getting processed by the where clause? What can I do to make the params Rails readable again?</p>
  
  <p>UPDATE:</p>
  
  <pre><code>Started POST "/publications/search?scroll=active&amp;page=6" for 127.0.0.1 at 2013-0
  2-12 22:55:24 -0600
  Processing by PublicationsController#index as */*
  Parameters: {"scroll"=&gt;"active", "page"=&gt;"6"}
  </code></pre>
  
  <p>UPDATE 2:</p>
  
  <p>The problem is apparently stemming from <code>contentType</code>. When I remove it, then <code>q</code> is sent as a Rails parameter. Unfortunately, <code>q</code> is still in JSON, resulting in the error:</p>
  
  <pre><code>undefined method `with_indifferent_access' for #&lt;String:0x686d0a8&gt;
  </code></pre>
  
  <p>How can I convert JSON to a params hash?</p>