Rails服务器崩溃多个js调用

时间:2017-06-16 18:13:43

标签: javascript ruby-on-rails ruby mongodb

我正在构建一个灵活的报告功能,作为更大项目的一部分。我正在努力解决的特定部分适用于多次调用然后导致服务器崩溃。我正在使用mongoid和puma服务器 - 如果有其他信息我可以告诉我。

做了很多搜索 - 真的很难过这个。提前谢谢!

控制台错误消息 这只是错误的第一部分 - 它会在几个页面上显示"控制帧信息","其他运行时信息"以及"进程内存映射& #34;

123

相关页面js 脚本存储类别字段的值,并将所有选定的列值(在多选表单中)推送到数组中,并将这两个变量发送到get_filter_columns控制器操作

/usr/local/rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/forwardable.rb:228: [BUG] Segmentation fault at 0x00000000000038
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]

_filter_columns.html.erb 视图中的元素响应onchange js事件

<script>
    $(document).ready(function(){
        function get_filter_columns(filter_columns, category){
          $.get('reports/get_filter_columns',{filter_columns, category});
        };

        $(document).on('change', "#filter_columns", function() {
            var filter_column_array = [];
            var category = $("#report_category").val();
            $("#filter_columns .box2 option").each(function() {

                filter_column_array.push($(this).val());
            });
            get_filter_columns(filter_column_array, category);
        });  
   });
</script>

reports_controller.rb 由js调用控制器操作

<div id = "filter_columns" class="col-lg-5">
    <div class = "ibox-content report_builder_body">
        <h2>Filter Columns</h2>
        <%= simple_fields_for :filter_columns_nested_form do |ff| %>
            <%= ff.select( :filter_columns, (@category || Account).attribute_names.map{ |value| [value.to_s.underscore.humanize, value] }, {}, { :id => "filter_columns", :class => "form-control dual_select_filter_columns", :multiple => true })  %>
        <% end %>
    </div>
</div>

get_filter_columns.js.erb js模板由get_filter_columns控制器操作

呈现
    def get_filter_columns

        @category = (params[:category] || "Account").split(' ').collect(&:capitalize).join.constantize

        @filter_categories = (params[:filter_columns] || []).map{ |filter| filter }

        @filter_symbols = {}

        @filter_logic = ["Equal to", "Not equal to", "Less than", "Less than or equal to", "Greater than", "Greater than or equal to", "Is between", "Is not between"]

        @filter_categories.each do |fs|
          if @category.instance_methods(false).include?(fs.to_s.to_sym) == true
            @filter_symbols[fs] = "enum"
          elsif @category.pluck(fs.to_s.to_sym)[0].class.to_s == "BSON::ObjectId"
            @filter_symbols[fs] = "string"
          elsif @category.pluck(fs.to_s.to_sym)[0].class.to_s == "NilClass"
            @filter_symbols[fs] = "string"
          elsif @category.pluck(fs.to_s.to_sym)[0].class.to_s == "Time"
            @filter_symbols[fs] = "date"
          else
            @filter_symbols[fs] = @category.pluck(fs.to_s.to_sym)[0].class.to_s || "string"
          end
        end

        render "reports/js_templates/get_filter_columns.js.erb"
    end

_filters.html.erb 部分由get_filter_columns.js.erb文件呈现

$('#filters').replaceWith('<%= j render("reports/partials/filters") %>');

2 个答案:

答案 0 :(得分:0)

这可能有助于错误,在控制器代码执行此操作时,它将至少阻止多个DB命中连续多次执行。

ClassOverrideList = {
  "BSON::ObjectId" => "string",
  "NilClass" => "string",
  "Time" => "date"          
}

def get_filter_columns

    @category = (params[:category] || "Account").split(' ').collect(&:capitalize).join.constantize

    @filter_categories = (params[:filter_columns] || []).map{ |filter| filter }

    @filter_symbols = {}

    @filter_logic = ["Equal to", "Not equal to", "Less than", "Less than or equal to", "Greater than", "Greater than or equal to", "Is between", "Is not between"]

    @filter_categories.each do |fs|
      fs_sym = fs.to_s.to_sym
      if @category.instance_methods(false).include?(fs.to_s.to_sym) == true
        @filter_symbols[fs] = "enum"
      else
        category_class = @category.pluck(fs_sym)[0].class.to_s
        @filter_symbols[fs] = ClassOverrideList[category_class] ||
          category_class ||
          "string"
      end
    end

    render "reports/js_templates/get_filter_columns.js.erb"
end

答案 1 :(得分:0)

这个问题似乎是通过js快速连续调用.class和.instance_methods方法引起的。我从控制器操作中删除了.class和.instance_methods方法,并且从那时起就没有问题。

我通过在初始页面加载时编译嵌套哈希来解决了这个问题(格式如下所示)。然后将散列传递回get_filter_columns控制器操作,以避免重新创建散列,并且必须在控制器内调用有问题的方法。

column_symbols = { model_1_name: { column_1_name: data_type, column_2_name: data_type }, model_2_name: { column_1_name: data_type, column_2_name: data_type }..... }