title: How to return an object from controller to ajax in rails
body: <p>I am creating dashboard in ROR.  I have a combobox which onchange sends the ajax get to the dashboard to get the data accordingly. Based on my value selected i will get the data and i will render the values in highcharts accordingly.</p>
  
  <pre><code>hbar.html.erb
  
      &lt;select  name="hBarParam" id="hBarParam" class="hBarParam"&gt;
          &lt;option value="Nothing"&gt;&lt;/option&gt;
          &lt;option value="Today" class="selected"&gt;Today&lt;/option&gt;
          &lt;option value="Weekly"&gt;Weekly&lt;/option&gt;
          &lt;option value="Monthly"&gt;Monthly&lt;/option&gt;
       &lt;/select&gt;
  
      &lt;div id="postsHBar" style="width:100%; height:500px" class="postsHBar"&gt;&lt;/div&gt;
      $(".hBarParam").change(function(){
  
            var str = $( "#hBarParam option:selected" ).text();
  
              $.get("/dashboard/hbar?hBarParam="+str,function( data){
  
                alert(&lt;%= @postsHBarClean.map{|p|p[1] } %&gt;)
  
              }).done(function() {
                alert(&lt;%= @postsHBarClean.map{|p|p[1] } %&gt;)
                //render new highcharts with different like postsHBarClean,        postsHBarSalvage,etc
  
    $( function () {
      new Highcharts.Chart({
            chart: {
                type: 'bar',
                renderTo: 'postsHBar',
                backgroundColor:null
  
            },
            title: {
                text:...
            },
            xAxis: {
                categories: &lt;%= raw @postsHBar.map{|p| p.make_vehicle}.map(&amp;:to_s) %&gt;,
  
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Count (Thousands)',
                    align: 'high'
                }...
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -40,
               ....
            },
            credits: {
                enabled: false
            },
           series: [{
            name: "Clean",
            data: &lt;%= @postsHBarClean.map{|p|p[1] } %&gt;
  
        },
        {
            name: "Salvage",
            data: &lt;%= @postsHBarSalvage.map{|p|p[1] } %&gt;
  
        }.....]
        });
    });    
              });
            });
  </code></pre>
  
  <p>Controller code </p>
  
  <pre><code>    class DashboardController &lt; ApplicationController
        respond_to :html, :js, :json
        # GET /dashboard/hbar
        def hbar
          puts params["hBarParam"]
          if params["hBarParam"] == "Today"
               postsHBarParam = "created_day = 26"
  
          elsif params["hBarParam"] == "Monthly"
            postsHBarParam = "created_month = 'April'"
          else
           postsHBarParam = "created_day between 22 and 27"
          end
          puts postsHBarParam
       #   postsHBarParam = "created_day = 26"
  
  
          @postsHBarClean = Post.where(postsHBarParam + " and title_status like ? and make_vehicle is not null", "clean").group(:make_vehicle).count.to_a
          @postsHBarSalvage = Post.where(postsHBarParam +" and title_status like ? and make_vehicle is not null", "salvage").group(:make_vehicle).count.to_a
          @postsHBarPartsOnly = Post.where(postsHBarParam +" and title_status like ? and make_vehicle is not null", "parts only").group(:make_vehicle).count.to_a
          @postsHBarRebuilt = Post.where(postsHBarParam +" and title_status like ? and make_vehicle is not null", "rebuilt").group(:make_vehicle).count.to_a
          @postsHBarLien = Post.where(postsHBarParam +" and title_status like ? and make_vehicle is not null", "lien").group(:make_vehicle).count.to_a
          @postsHBar = Post.select(:make_vehicle).where(postsHBarParam +" and title_status is not null and make_vehicle is not null" ).uniq
          puts @postsHBarClean
  
      respond_to do |format|
       format.js
           format.html
         end
  end
  </code></pre>
  
  <p>The problem is that the chart renders.. but with default values (i.e else part in controller) so whenever i change the select option .. 'puts string' works but charts takes the same values i.e those that were during the page load.
  There is no syntax error or brackets missing :P</p>
  
  <p>I am badly stuck at the issue and dont know how to resolve it. :(</p>