0

我一直在尝试按照这些说明在 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 %>

编辑:

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

第 31 行:@sbcons = @sbcons.where(sbcon_type: filters[:type]) 除非 filters[:type].nil? 或过滤器[:类型] .blank?

第 26 行:respond_to do |format|

4

1 回答 1

2

将所有业务逻辑移出块并移出块上方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

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

于 2013-06-06T14:44:08.173 回答