首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NoMethodError (nil:NilClass的未定义方法`[]‘):with jquery-datatables-rails

NoMethodError (nil:NilClass的未定义方法`[]‘):with jquery-datatables-rails
EN

Stack Overflow用户
提问于 2013-06-06 21:15:35
回答 1查看 966关注 0票数 0

我一直在尝试通过遵循这些instructions在jquery-datatables上进行过滤。

在我添加过滤器之前,它返回的是datatables所需的json,没有任何问题,但现在我得到了上面的错误,无法识别问题。

代码语言:javascript
复制
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 %>

编辑:

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

第31行:@sbcons = @sbcons.where(sbcon_type: filters:type),除非filters:type.nil?或者是filters:type.blank?

第26行: respond_to do |format|

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-06 22:44:08

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

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

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

索引操作应如下所示:

代码语言:javascript
复制
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

这段代码还远未优化,但它应该能满足这个问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16963134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档