title: Pass Ruby Array via Ajax and Parse
body: <p>I'm working on a Rails project in which I have a somewhat of an odd situation where I need to load a template (via ajax) into an existing page and populate the template with values from a Ruby array sent from that page. Here's my example:</p>
  
  <p>Article Page:</p>
  
  <pre><code>&lt;div class="article"&gt;&lt;/div&gt;
  
  &lt;% @article = my_article_array.to_json %&gt;
  
  &lt;script type="text/javascript" charset="utf-8"&gt;
       $(document).ready(function() {
              $.ajax({
                type: 'post',
                data: { 'article' : '&lt;%= @article %&gt; }, 
                url: '/articles/show',
                    success: function(result) {
                       $(".article").html(result);
                    }
              });
       });   
  &lt;/script&gt;
  </code></pre>
  
  <p>Controller: </p>
  
  <pre><code>def show
     @article = JSON.parse(params[:article])
     redner :template =&gt; false
  end
  </code></pre>
  
  <p>Template:</p>
  
  <pre><code>&lt;h1&gt;&lt;%= @article.title %&gt;&lt;/h1&gt;
  &lt;p&gt;&lt;%= @article.body %&gt;&lt;/p&gt;
  </code></pre>
  
  <p>This results in a Rails error: "JSON.parse unexpected token error".</p>
  
  <p>I've tried dozens of different scenarios, but converting the array to JSON first then decoding it once passed along seemed to make the most sense. Regardless, none of my attempts have worked.</p>
  
  <p>Any insights would be much appreciated!</p>