来自控制器的Rails参数无法在视图中显示

时间:2016-05-05 10:07:19

标签: ruby-on-rails ajax

我使用ajax从datepicker获取用户选择的日期,然后将它们传递给控制器​​以计算结果,然后将结果传回视图。

模型中,我有:

def mobile_type_count(name,name1,field, 
    filed1,value, start_time, end_time )
 ........
end

控制器 analyzer_controller.rb

def data

start_time = params[:start_date]?
        Date.parse(params[:start_date]) :Date.iso8601
end_time =  params[:end_date]?
        Date.parse(params[:end_date]) :Date.iso8601

     @iphone =Analyzer.new.
      mobile_type_count('deviceOS','mobileNum','$deviceOS',
                        '$mobileNum','iOS' , start_time , end_time  )
      @android = Analyzer.new.
      mobile_type_count('deviceOS','mobileNum','$deviceOS',
                        '$mobileNum','Android' ,  start_time, end_time )

 end

查看 data.html.erb

<div class="input-daterange input-group" id="datepicker">
    <input type="text" class="input-sm form-control" name="start" value="2016-04-11" id="startDate" />
    <span class="input-group-addon">to</span>
    <input type="text" class="input-sm form-control" name="end" value="2016-04-13" id="endDate" />
  </div>
    <%=   button_tag(type: 'submit', class: "btn btn-default btn-sm", :id => 'get_data' ) do %>
        Submit

    <% end %>
</div>
<div class="ibox-content">
      <%=  pie_chart({"iphone" => @iphone, "android" => @android}) %>
    </div>

的Ajax

<script type="text/javascript">


  $(document).ready(function() {

  $('#get_data').click(function(){
        var sd = $('#startDate').val(),
            ed = $('#endDate').val()
    $.ajax({
      url: "/analyzer/data",
      type: "post",
      data: {'start_date' :sd, 'end_date': ed},

      success: function(){
      },
      error:function(){
        alert('Error');
      }
    });
  });

当我选择日期范围并单击“提交”按钮时,我什么都没得到,但是什么时候 我使用 puts 来测试所选日期是否已传递给控制器​​

 <%=  puts  pie_chart({"iphone" => @iphone, "android" => @android}) %>

我可以在终端上打印结果,

<script type="text/javascript">
  new Chartkick.PieChart("chart-1", {"iphone":[11],"android":[10]}, {});

@iphone = [11] @android = [10],这是正确的,

当我删除puts时,然后在浏览器中检查结果,@ iphone和@android是空的arraies:

<script type="text/javascript">
  new Chartkick.PieChart("chart-1", {"iphone":[],"android":[]}, {});
</script>

不确定发生了什么,希望有人可以向我解释一下。谢谢

已更新
这是the mobile_type_count方法,但我确定方法没问题,否则<%= puts pie_chart({"iphone" => @iphone, "android" => @android}) %>不会返回任何结果。我使用puts的原因仅仅是测试,我认为问题是结果(@iphone,@ andodod)没有在视图中呈现

def mobile_type_count(name,name1,field, filed1,value, start_time, end_time )

date_match = {:$match  => {:created_at => { :$gt =>  start_time , :$lt => end_time  }}}
match = { :$match => {name => value}}
group ={:$group =>{ :_id =>
                         {name => field,
                          name1 => filed1},
                     :count => {:$sum =>1}
}}
group1 = {:$group =>{:_id =>
                          { name => field,
                            name1 => filed1},
                      #:totalCount=> { :$sum => '$count' },
                      :distinctcount => {:$sum => 1}
}}

mobile_usr= collection.aggregate([date_match,match,group,group1])

mobile_usr.map do |m|
  m[:distinctcount]
end
  end

1 个答案:

答案 0 :(得分:1)

从下面的行中删除puts。否则,您将呈现给$stdout

<%=  puts  pie_chart({"iphone" => @iphone, "android" => @android}) %>

应该是:

<%=  pie_chart({"iphone" => @iphone, "android" => @android}) %>