jhund / filterrific

Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
http://filterrific.clearcove.ca
MIT License
910 stars 124 forks source link

Filter fields not working - spinner shows but nothing happens? #127

Closed jimmiejackson414-zz closed 7 years ago

jimmiejackson414-zz commented 7 years ago

I've been pouring over this issue for weeks avoiding having to post here, but I just can't seem to work this issue out! (PS I'm relatively new to Rails but am soaking up as much as humanly possible). I have three search parameters: a regular search text field, a dropdown to filter by university and another one to filter by program. Trying to filter by any one of those three brings the spinner up, but nothing actually happens. I only have 5 entries as dummy data in my db, so I know it's not an issue with returning too many results or something of that nature. Hopefully it's something simple and I can feel dumb for a few minutes before moving on! Thanks in advance for your help.

Filterrific: 2.1.2 Rails: 4.1.9 Ruby: 2.3.3

User Model

filterrific(
    # default_filter_params: { sorted_by: 'created_at_desc' },
    available_filters: [
      # :sorted_by,
      :search_query,
      :with_university_id,
      :with_program_id
      # :admin,
      # :student
    ]
  )

  has_many :typing_tests
  belongs_to :university
  has_many :programs, through: university

# default for will_paginate
  self.per_page = 10

  scope :search_query, lambda { |query|
    return nil  if query.blank?
    # condition query, parse into individual keywords
    terms = query.downcase.split(/\s+/)
    # replace "*" with "%" for wildcard searches,
    # append '%', remove duplicate '%'s
    terms = terms.map { |e|
      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }
    # configure number of OR conditions for provision
    # of interpolation arguments. Adjust this if you
    # change the number of OR conditions.   
    num_or_conditions = 2
    where(
    terms.map { |term|
      "(LOWER(users.first_name) LIKE ? OR LOWER(users.last_name) LIKE ?)"
    }.join(' AND '),
    *terms.map { |e| [e] * num_or_conds }.flatten
  )

  }

  scope :with_university_id, lambda { |university_ids|
    where(university_ids: [*university_ids])
  }

  scope :with_program_id, lambda { |program_ids|
    where(program_ids: [*program_ids])
  }

University Model

class University < ActiveRecord::Base

    has_many :programs
    has_many :users, through: :programs

    def self.options_for_select
        order('LOWER(name)').map { |e| [e.name, e.id] }
    end
end

Program Model


    belongs_to :university
    has_many :users

    def self.options_for_select
        order('LOWER(name)').map { |e| [e.name, e.id] }
    end

end

Users Controller

class UsersController < ApplicationController
  before_action :set_user, only: [:edit, :update, :destroy]
  before_filter :authenticate_user!
  before_action :check_if_admin

  # GET /users
  # GET /users.json
  def index
    @filterrific = initialize_filterrific(
      User,
      params[:filterrific],
      :select_options => {
        with_university_id: University.options_for_select,
        with_program_id: Program.options_for_select,
        # with_role: @roles.options_for_select
      },
    ) or return
    @users = @filterrific.find.page(params[:page])

    respond_to do |format|
      format.html
      format.js
    end

    rescue ActiveRecord::RecordNotFound => e
      # There is an issue with the persisted param_set. Reset it.
      puts "Had to reset filterrific params: #{ e.message }"
      redirect_to(reset_filterrific_url(format: :html)) and return
    end
end

Users Index

    <%= form_for_filterrific @filterrific do |f| %>
        <div class='row'>
            <div class='col-md-3'>
                <%= f.label :search_query %>
                <%= f.text_field(
                    :search_query,
                    class: 'filterrific-periodically-observed form-control'
                ) %>
            </div>
            <div class='col-md-3'>
                <%= f.label :with_program_id, 'Program' %>
                <%= f.select(
                    :with_program_id,
                    @filterrific.select_options[:with_program_id],
                    { include_blank: '- Select One -' },
                    { :class => 'form-control' }
                ) %>
            </div>
            <div class='col-md-3'>
                <%= f.label :with_university_id, 'University' %>
                <%= f.select(
                    :with_university_id,
                    @filterrific.select_options[:with_university_id],
                    { include_blank: '- Select One -' },
                    { :class => 'form-control' }
                ) %>
            </div>
            <div class='col-md-3' style='padding-top:2em;'>
                <div style='float:left;'>
                    <%= f.label :with_role, 'Admin', class: 'margin-left' %>
                    <%#= f.check_box ':with_role', class: 'filterrific-periodically-observed float-left' %>  
                </div>
                <div style='float:left; margin-left:3em;'>
                    <%= f.label :with_role, 'Student', class: 'margin-left' %>
                    <%#= f.check_box ':with_role', class: 'filterrific-periodically-observed float-left' %>  
                </div>
            </div>
        </div>

        <div class="spinner">
            <%# add an automated spinner to your form when the list is refreshed %>
            <%= render_filterrific_spinner %>  
        </div>
    <% end %>

</div>

<div class='well well-small'>
    <div class="pull-left">
        <%= page_entries_info @users, :model => 'user' %>
    </div>
    <div class='pull-right'>
        <%= link_to 'Reset filters', reset_filterrific_url, class: 'btn btn-trilogy' %>
    </div>
    <div style='clear:both;'></div>
</div>
<%= render(
    partial: 'users/list',
    locals: { users: @users }
) %>

List Partial

<div id="filterrific_results">

  <table class='table table-hover'>
    <thead>
      <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Role</th>
        <th>Program</th>
        <th>University</th>
        <th>Best WPM</th>
        <th>Registered at</th>
      </tr>
    </thead>
    <% @users.each do |user| %>
      <tr>
        <td class='center'><%= link_to(user.full_name, user_path(user)) %></td>
        <td class='center'><%= user.email %></td>
        <td class='center'><%= user.role.capitalize %></td>
        <td class='center'><%= user.programs.name %></td>
        <td class='center'><%= user.university.name %></td>
        <% if user.best_wpm == 0 %>
          <td class='center'>None taken</td>
        <% else %>
          <td class='center'><%= user.best_wpm %></td>
        <% end %>
        <td class='center'><%= user.decorated_created_at %></td>
      </tr>
    <% end %>
  </table>
</div>

<%= will_paginate @users %>

Finally my stack trace from terminal:

Started GET "/users?utf8=%E2%9C%93&filterrific%5Bsearch_query%5D=&filterrific%5Bwith_program_id%5D=3&filterrific%5Bwith_university_id%5D=&_=1492142337559" for 127.0.0.1 at 2017-04-13 22:59:19 -0500
Processing by UsersController#index as JS
  Parameters: {"utf8"=>"✓", "filterrific"=>{"search_query"=>"", "with_program_id"=>"3", "with_university_id"=>""}, "_"=>"1492142337559"}
  User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 2  ORDER BY "users"."id" ASC LIMIT 1
  University Load (0.3ms)  SELECT "universities".* FROM "universities"   ORDER BY LOWER(name)
  Program Load (0.3ms)  SELECT "programs".* FROM "programs"   ORDER BY LOWER(name)
   (0.8ms)  SELECT COUNT(*) FROM "users"  WHERE "users"."program_ids" IN (3)
PG::UndefinedColumn: ERROR:  column users.program_ids does not exist
LINE 1: SELECT COUNT(*) FROM "users"  WHERE "users"."program_ids" IN...
                                            ^
HINT:  Perhaps you meant to reference the column "users.program_id".
: SELECT COUNT(*) FROM "users"  WHERE "users"."program_ids" IN (3)
  Rendered users/index.html.erb (7.1ms)
Completed 500 Internal Server Error in 25ms

ActiveRecord::StatementInvalid - PG::UndefinedColumn: ERROR:  column users.program_ids does not exist
LINE 1: SELECT COUNT(*) FROM "users"  WHERE "users"."program_ids" IN...
                                            ^
HINT:  Perhaps you meant to reference the column "users.program_id".
: SELECT COUNT(*) FROM "users"  WHERE "users"."program_ids" IN (3):
  activerecord (4.1.9) lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `async_exec'
  activerecord (4.1.9) lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `block in exec_no_cache'
  activerecord (4.1.9) lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
  activesupport (4.1.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activerecord (4.1.9) lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
  activerecord (4.1.9) lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `exec_no_cache'
  activerecord (4.1.9) lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
  activerecord (4.1.9) lib/active_record/connection_adapters/postgresql_adapter.rb:954:in `select'
  activerecord (4.1.9) lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
  activerecord (4.1.9) lib/active_record/connection_adapters/abstract/query_cache.rb:68:in `block in select_all'
  activerecord (4.1.9) lib/active_record/connection_adapters/abstract/query_cache.rb:83:in `cache_sql'
  activerecord (4.1.9) lib/active_record/connection_adapters/abstract/query_cache.rb:68:in `select_all'
  activerecord (4.1.9) lib/active_record/relation/calculations.rb:265:in `execute_simple_calculation'
  activerecord (4.1.9) lib/active_record/relation/calculations.rb:227:in `perform_calculation'
  activerecord (4.1.9) lib/active_record/relation/calculations.rb:119:in `calculate'
  activerecord (4.1.9) lib/active_record/relation/calculations.rb:34:in `count'
  will_paginate (3.1.5) lib/will_paginate/active_record.rb:89:in `count'
  will_paginate (3.1.5) lib/will_paginate/active_record.rb:87:in `count'
  will_paginate (3.1.5) lib/will_paginate/active_record.rb:74:in `total_entries'
  will_paginate (3.1.5) lib/will_paginate/collection.rb:16:in `total_pages'
  will_paginate (3.1.5) lib/will_paginate/view_helpers.rb:124:in `page_entries_info'
  will_paginate (3.1.5) lib/will_paginate/view_helpers/action_view.rb:40:in `page_entries_info'
  app/views/users/index.html.erb:53:in `_app_views_users_index_html_erb__3532746461584971822_70222776323200'
  actionview (4.1.9) lib/action_view/template.rb:145:in `block in render'
  activesupport (4.1.9) lib/active_support/notifications.rb:161:in `instrument'
  actionview (4.1.9) lib/action_view/template.rb:339:in `instrument'
  actionview (4.1.9) lib/action_view/template.rb:143:in `render'
  actionview (4.1.9) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
  actionview (4.1.9) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  activesupport (4.1.9) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.9) lib/active_support/notifications.rb:159:in `instrument'
  actionview (4.1.9) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
  actionview (4.1.9) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
  actionview (4.1.9) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
  actionview (4.1.9) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
  actionview (4.1.9) lib/action_view/renderer/template_renderer.rb:17:in `render'
  actionview (4.1.9) lib/action_view/renderer/renderer.rb:42:in `render_template'
  actionview (4.1.9) lib/action_view/renderer/renderer.rb:23:in `render'
  actionview (4.1.9) lib/action_view/rendering.rb:99:in `_render_template'
  actionpack (4.1.9) lib/action_controller/metal/streaming.rb:217:in `_render_template'
  actionview (4.1.9) lib/action_view/rendering.rb:82:in `render_to_body'
  actionpack (4.1.9) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
  actionpack (4.1.9) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
  actionpack (4.1.9) lib/abstract_controller/rendering.rb:25:in `render'
  actionpack (4.1.9) lib/action_controller/metal/rendering.rb:16:in `render'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
  activesupport (4.1.9) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
  /Users/jimmiejackson/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
  activesupport (4.1.9) lib/active_support/core_ext/benchmark.rb:12:in `ms'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
  activerecord (4.1.9) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:40:in `render'
  actionpack (4.1.9) lib/action_controller/metal/mime_responds.rb:258:in `respond_to'
  app/controllers/users_controller.rb:20:in `index'
  actionpack (4.1.9) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.1.9) lib/abstract_controller/base.rb:189:in `process_action'
  actionpack (4.1.9) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.1.9) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.1.9) lib/active_support/callbacks.rb:113:in `call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.9) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.9) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.9) lib/active_support/callbacks.rb:229:in `block in halting'
  activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.9) lib/active_support/callbacks.rb:86:in `run_callbacks'
  actionpack (4.1.9) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.1.9) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.1.9) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.9) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.1.9) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.1.9) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.1.9) lib/abstract_controller/base.rb:136:in `process'
  actionview (4.1.9) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.1.9) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.1.9) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.1.9) lib/action_controller/metal.rb:232:in `block in action'
  actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
  actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:50:in `call'
  actionpack (4.1.9) lib/action_dispatch/journey/router.rb:73:in `block in call'
  actionpack (4.1.9) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.1.9) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:685:in `call'
  meta_request (0.3.4) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
  meta_request (0.3.4) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
  warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.6) lib/warden/manager.rb:34:in `catch'
  warden (1.2.6) lib/warden/manager.rb:34:in `call'
  rack (1.5.5) lib/rack/etag.rb:23:in `call'
  rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.5) lib/rack/head.rb:11:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/flash.rb:254:in `call'
  rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/cookies.rb:562:in `call'
  activerecord (4.1.9) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.1.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  activerecord (4.1.9) lib/active_record/migration.rb:380:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.9) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  rack-contrib (1.2.0) lib/rack/contrib/response_headers.rb:17:in `call'
  meta_request (0.3.4) lib/meta_request/middlewares/headers.rb:16:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.9) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.9) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.9) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.9) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  actionpack (4.1.9) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.5) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.9) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.5) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/static.rb:84:in `call'
  rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.9) lib/rails/engine.rb:514:in `call'
  railties (4.1.9) lib/rails/application.rb:144:in `call'
  rack (1.5.5) lib/rack/lock.rb:17:in `call'
  rack (1.5.5) lib/rack/content_length.rb:14:in `call'
  rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
  /Users/jimmiejackson/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
  /Users/jimmiejackson/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
  /Users/jimmiejackson/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'
jimmiejackson414-zz commented 7 years ago

Got it worked out myself!