0

I have been trying to get filtering working on jquery-datatables by following these instructions.

Before I added the filters it was returning the json needed by datatables without any issue but now I'm getting the above error and cannot identify the issue.

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 %>

EDIT:

NoMethodError (undefined method []' for nil:NilClass): app/controllers/subcontractors_controller.rb:31:inblock (2 levels) in index' app/controllers/subcontractors_controller.rb:26:in `index'

Line 31: @sbcons = @sbcons.where(sbcon_type: filters[:type]) unless filters[:type].nil? or filters[:type].blank?

Line 26: respond_to do |format|

6
  • 1
    can you post the stack trace , at which line it says the error Commented Jun 6, 2013 at 13:28
  • 1
    Also, filters[:type].blank? will test for .nil? too: nil.blank? #=> true (and .present? is the opposite of .blank?) Commented Jun 6, 2013 at 13:29
  • I've updated the question with the trace and thanks for the information about the .nils being extraneous. Commented Jun 6, 2013 at 13:33
  • filters is nil on line 31. Commented Jun 6, 2013 at 14:22
  • 1
    No, but filters.nil? will. Commented Jun 6, 2013 at 14:28

1 Answer 1

2

Move all of the business logic out of and above the respond_to block. That block should only contain code necessary for rendering views.

Also, you don't need both .nil? and .blank? conditionals. .blank? checks for nil anyway.

Check if filters is nil once before doing any further setting. Use a conditional block as shown.

Your index action should look like this:

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

This code is still far from optimized, but it should satisfy the question.

Sign up to request clarification or add additional context in comments.

1 Comment

That's fixed it unfortunately now render json: SubcontractorsDatatable.new(view_context, @sbcons) is throwing ArgumentError (wrong number of arguments (2 for 1))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.