How to return an object from controller to ajax in rails -


i creating dashboard in ror. have combobox onchange sends ajax dashboard data accordingly. based on value selected data , render values in highcharts accordingly.

hbar.html.erb      <select  name="hbarparam" id="hbarparam" class="hbarparam">         <option value="nothing"></option>         <option value="today" class="selected">today</option>         <option value="weekly">weekly</option>         <option value="monthly">monthly</option>      </select>      <div id="postshbar" style="width:100%; height:500px" class="postshbar"></div>     $(".hbarparam").change(function(){            var str = $( "#hbarparam option:selected" ).text();              $.get("/dashboard/hbar?hbarparam="+str,function( data){                alert(<%= @postshbarclean.map{|p|p[1] } %>)              }).done(function() {               alert(<%= @postshbarclean.map{|p|p[1] } %>)               //render new highcharts different postshbarclean,        postshbarsalvage,etc    $( function () {     new highcharts.chart({           chart: {               type: 'bar',               renderto: 'postshbar',               backgroundcolor:null            },           title: {               text:...           },           xaxis: {               categories: <%= raw @postshbar.map{|p| p.make_vehicle}.map(&:to_s) %>,            },           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: <%= @postshbarclean.map{|p|p[1] } %>        },       {           name: "salvage",           data: <%= @postshbarsalvage.map{|p|p[1] } %>        }.....]       });   });                 });           }); 

controller code

    class dashboardcontroller < applicationcontroller       respond_to :html, :js, :json       # /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 , 27"         end         puts postshbarparam      #   postshbarparam = "created_day = 26"           @postshbarclean = post.where(postshbarparam + " , title_status ? , make_vehicle not null", "clean").group(:make_vehicle).count.to_a         @postshbarsalvage = post.where(postshbarparam +" , title_status ? , make_vehicle not null", "salvage").group(:make_vehicle).count.to_a         @postshbarpartsonly = post.where(postshbarparam +" , title_status ? , make_vehicle not null", "parts only").group(:make_vehicle).count.to_a         @postshbarrebuilt = post.where(postshbarparam +" , title_status ? , make_vehicle not null", "rebuilt").group(:make_vehicle).count.to_a         @postshbarlien = post.where(postshbarparam +" , title_status ? , make_vehicle not null", "lien").group(:make_vehicle).count.to_a         @postshbar = post.select(:make_vehicle).where(postshbarparam +" , title_status not null , make_vehicle not null" ).uniq         puts @postshbarclean      respond_to |format|      format.js          format.html        end end 

the problem chart renders.. default values (i.e else part in controller) whenever change select option .. 'puts string' works charts takes same values i.e during page load. there no syntax error or brackets missing :p

i badly stuck @ issue , dont know how resolve it. :(

if using ajax call should answer js.

if understood right, controller should send ajax call. controller should render json. like:

class dashboardcontroller < applicationcontroller   ...   respond_to |format|     format.js { render: postshbarclean.to_json }     format.html   end end 

and javascript should handle json response.

take time read guide: http://guides.rubyonrails.org/working_with_javascript_in_rails.html , watch railscast: http://railscasts.com/episodes/136-jquery-ajax-revised


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -