【问题标题】:NoMethodError (undefined method `[]' for nil:NilClass): with jquery-datatables-railsNoMethodError(nil:NilClass 的未定义方法“[]”):使用 jquery-datatables-rails
【发布时间】:2013-06-06 13:15:34
【问题描述】:

我一直在尝试通过关注这些instructions 来对 jquery-datatables 进行过滤。

在我添加过滤器之前,它返回数据表所需的 json 没有任何问题,但现在我收到上述错误并且无法识别问题。

def index
    respond_to do |format|
      format.html
      format.json do
        @sbcons = Subcontractor.scoped
        filters = params[:filter]
        @sbcons = @sbcons.where(sbcon_type: filters[:type]) unless filters[:type].nil? or filters[:type].blank?
        @sbcons = @sbcons.where(cscs_card: filters[:cscs]) unless filters[:cscs].nil? or filters[:cscs].blank?
        @sbcons = @sbcons.where(approved_status: filters[:approved]) unless filters[:approved].nil? or filters[:approved].blank?
        render json: SubcontractorsDatatable.new(view_context, @sbcons)
      end
    end
  end

<% provide(:title, 'All Subcontractors') %>
<h1>Subcontractors List</h1>
<div class="filter">
  <%= form_tag(method: :get, id: "filter_form") do %>
    <%= label_tag :sbcon_type, "Type" %>
    <%= select_tag "filter[type]", options_for_select([[],["Labour Only"], ["Specialist"], ["Both"]]) %>
    <%= label_tag :cscs_card, "CSCS" %>
    <%= select_tag "filter[cscs]", options_for_select([[],["Yes"], ["No"]]) %>
    <%= label_tag :approved_status, "Approved Status" %>
    <%= select_tag "filter[status]", options_for_select([[],["Approved"], ["Not Approved"]]) %> <br>
    <%= link_to "Filter", '#', id: "filterbutton", class: "btn btn-mini" %>
  <% end %>
  <br>
</div>
<table id="subcontractors" class="table table-condensed table-hover display" data-source="<%= subcontractors_url(format: "json") %>">
  <thead>
    <tr>
      <th>Name</th>
      <th>Contact Number</th>
      <th>CSCS</th>
      <th>Type</th>
      <th>Scotland</th>
      <th>NE England</th>
      <th>NW England</th>
      <th>Midlands</th>
      <th>SE England</th>
      <th>SW England</th>
      <th>London</th>
      <th>Wales</th>
      <th>Operatives</th>
      <th>Product Liability</th>
      <th>Employer Liability</th>
      <th>Public Liability</th>
      <th>Contractors All Risk</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

<%= javascript_tag do %> 
  $('#filterbutton').click(function (){
    $('#subcontractors').dataTable().fnDraw();
  });
<% end %>

编辑:

NoMethodError(索引中未定义的方法[]' for nil:NilClass): app/controllers/subcontractors_controller.rb:31:inblock(2 级)' app/controllers/subcontractors_controller.rb:26:in `index'

第 31 行:@sbcons = @sbcons.where(sbcon_type: filters[:type]) unless filters[:type].nil?还是过滤器[:type].blank?

第 26 行:respond_to do |format|

【问题讨论】:

  • 你能发布堆栈跟踪,它在哪一行显示错误
  • 另外,filters[:type].blank? 也将测试.nil?nil.blank? #=&gt; true(而.present?.blank? 的反面)
  • 我已用跟踪更新了问题,并感谢有关 .nils 无关的信息。
  • filters 在第 31 行为零。
  • 不会,但filters.nil? 会。

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

将所有业务逻辑移出respond_to 块。该块应该只包含渲染视图所需的代码。

此外,您不需要同时使用 .nil?.blank? 条件。 .blank? 无论如何都会检查 nil。

在进行任何进一步设置之前检查filters 是否为 nil 一次。如图所示使用条件块。

您的索引操作应如下所示:

def index
  @sbcons = Subcontractor.scoped
  if filters = params[:filter]
    @sbcons = @sbcons.where(sbcon_type: filters[:type]) unless filters[:type].blank?
    @sbcons = @sbcons.where(cscs_card: filters[:cscs]) unless filters[:cscs].blank?
    @sbcons = @sbcons.where(approved_status: filters[:approved]) unless filters[:approved].blank?
  end
  respond_to do |format|
    format.html
    format.json do
      render json: SubcontractorsDatatable.new(view_context, @sbcons)
    end
  end
end

这段代码还远未优化,但应该可以满足问题。

【讨论】:

  • 不幸的是现在渲染 json:SubcontractorsDatatable.new(view_context, @sbcons) is throwing ArgumentError (error number of arguments (2 for 1))
猜你喜欢
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-14
  • 2013-01-14
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多